Separating text from a string into an array

6

I need a way to separate a string like the following:

  

"0.1253729 09863637 02937382 029828020"

I want to remove each of the information and store it in an array .

obs: I'm reading from .txt file .

    
asked by anonymous 08.08.2017 / 20:05

3 answers

9

Use the Java split function.

As a parameter of the split function, you pass the separator that in the case is the space.

String foo = "Palavra1 Palavra2 Palavra3";
String[] split = foo.split(" ");
split[0] = "Palavra1";
split[1] = "Palavra2";
split[2] = "Palavra3";
    
08.08.2017 / 20:07
7

That way, here too it works, using regex and Stream :

String str = "0.1253729 09863637 02937382 029828020";

String[] array = Pattern.compile("\s+")//regex que filtra um ou mais espaços
                               .splitAsStream(str)//quebra a string em um array de Stream
                               .toArray(String[]::new);//converte num array de String

See working on ideone .

    
08.08.2017 / 20:25
3

So I understand you want to break the string in the "", which would be the range.

You can use the split function:

String[] strRetorno = "0.1253729 09863637 02937382 029828020".split(" ");
    
08.08.2017 / 20:09