In a String separates with data separated by ";" how to get the 2nd data?

0

I have the string x="Alberto; jose; Felipe; Ana". I needed to get the 2nd die, how do I do it? Thank you in advance.

obs: The string x is inside an array of strings.

    
asked by anonymous 14.04.2016 / 21:36

3 answers

3
"Alberto;jose;Felipe;Ana".split(";")[1];

"split" returns an array with all strings resulting from the "break" in the character passed by parameter.

    
14.04.2016 / 21:39
2

To get data from a separate string by "; " you can use the split method of the string variable itself.

Example:

String x = "Alberto;jose;Felipe;Ana";

String[] dadosSeparados = x.split(";");

for(String s : dadosSeparados){
    System.out.println("dado : "+s);
}
    
14.04.2016 / 21:42
1

Use the split function of the string class.

String frase =  "Alberto;jose;Felipe;Ana";
String[] array = frase.split(";");
    
14.04.2016 / 21:39