Separation of string text into an array

Asked

Viewed 497 times

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 in one array.

Obs: I’m reading from file .txt.

  • 3

    Quotation marks are part of the text?

  • 1

    Is by line document, separate how? By space or point is also part, are only spaces or have Tabs? Could you detail more? Quotation marks are part of the text?

3 answers

9

Use the Java split function.

As a split function parameter, you pass the separator which in this case is the space.

String foo = "Palavra1 Palavra2 Palavra3";
String[] split = foo.split(" ");
split[0] = "Palavra1";
split[1] = "Palavra2";
split[2] = "Palavra3";

7

This way also works here, 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

Behold functioning on the ideone.

3

From what I understand you want to break the string no " ", that would be the interval.

You can use the split function:

String[] strRetorno = "0.1253729 09863637 02937382 029828020".split(" ");

Browser other questions tagged

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