Get a specific part of a string using ANDROID split method

0

I made a code to display processes in android but when printing in a text view, it prints the PID, process name, cpu memory usage etc. the view stays that way

958  2   0% S    22 577164K  42624K  bg u0_a63   com.snaptube.premium

I would like to print only "com.snaptube.premium"

I tried to use the split method but did not succeed

My code for showing processes is like this

  p = Runtime.getRuntime().exec(command); // uso para utilizar comandos do linux
  p.waitFor();
  String line = ""; // string que contem a linha com as informações do processo
  BufferedReader reader = new BufferedReader(new  InputStreamReader(p.getInputStream()));                      
  BufferedReader bufferedReader = new BufferedReader(inputStreamReader);            

                while ((line = reader.readLine()) != null) {
                System.out.println(line);
                output.append(line + "\n"); // impressao dos processos no textview

I created a string variable [] and assign it the line.lengh () - 1 and I used line.split ("") to separate the string by whitespace

    
asked by anonymous 16.10.2015 / 20:05

1 answer

1

The split function is not required. If it's the last part of the text, it's simpler to use substring methods. and lastIndexOf of class String

String parteFinal = texto.substring(texto.lastIndexOf(" ") + 1)

The substring (int start) method returns the string starting at the index passed to the end of the parameter.

To find out what the index is going to substring () we used the lastIndexOf (String string) method that returns the index of the last occurrence of the string passed as parameter.

                                                 _substring(start+1)_
                                                 |                  |
958  2   0% S    22 577164K  42624K  bg u0_a63   com.snaptube.premium
                                                ^
                                                |
                                              start = texto.lastIndexOf(" ")
    
16.10.2015 / 20:12