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

Asked

Viewed 75 times

0

I have the string x = "Alberto;Jose;Felipe;Ana". I needed to take the 2nd dice, how do I do that? I thank you already.

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

  • I’m not a Java programmer, but the logic you want to use is similar to reading CSV files, follow the link http://answall.com/questions/27013/como-ler-arquivos-csv-java

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.

  • exactly what I needed, forgot the detail that the split returns an array.

2

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

Example:

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

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

for(String s : dadosSeparados){
    System.out.println("dado : "+s);
}

1

Use the string class split function.

String frase =  "Alberto;jose;Felipe;Ana";
String[] array = frase.split(";");
  • String array[] shouldn’t be String[] array? And just one addendum, this line String[] array = new String[3]; is very prolific, you can do direct String[] array = frase.split(";");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.