0

I am using this to get each line from a text file using Java Class.

String line = null;
try{    
       BufferedReader Br=new BufferedReader(new FileReader("/data/local/fileconfig.txt"));   

       while ((line = Br.readLine()) != null)
        {
           Log.d("hi", "LINE : " + line);              
        }
     } catch (Throwable throwable) { }

The fileconfig.txt has 2 lines like this -

############################################
packagename:com.hello

So my Log is like -

LINE :  ############################################
LINE :  packagename:com.hello

My question is now I am able to get this line info packagename:com.hello. I just want to extract com.hello out of this line. What is the logic I can use in my while loop?

Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
FAZ
  • 255
  • 1
  • 3
  • 13

3 Answers3

3

You could use the startsWith() method.

Or, somewhat more powerful, but also more difficult, use Regular Expressions with Match Groups.

See here: Using Java to find substring of a bigger string using Regular Expression

Community
  • 1
  • 1
Rob Audenaerde
  • 19,195
  • 10
  • 76
  • 121
2

If the format of line is always the same then this should do:

Log.d("hi", "LINE : " + line.substring(line.lastIndexOf(":")+1));
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
1

You can use either RegEx or a simple split() from String

:?(.*)

or split

String myString = "dsd:Example";
myString.split(":");
Marcinek
  • 2,144
  • 1
  • 19
  • 25