0

I have a String as below

String combinedString = "10.15%34.23%67.8% 89.00%100.00%44.44%";

As you can see above, there is a space between 67.8% and 89.00%

I would want to split them into two String arrays or two strings as below

String[] firstStringArray = {10.15%,34.23%,67.8%};
String[] secondStringArray = {89.00%, 100.00%, 44.44%};

or

String firstString = "10.15%34.23%67.8%";
String secondString = "89.00%100.00%44.44%";

Any idea on this please?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 1
    Java has a .split function that takes in a delimeter. Please see https://stackoverflow.com/questions/35017480/split-string-into-array-based-on-multiple-delimiters-in-java –  Apr 12 '20 at 18:00
  • please share what you have tried so far – Nithin Apr 12 '20 at 18:00

2 Answers2

0

You can use the white-space regex to split string based on delimiter of space, snippet as below,

    String str = "10.15%34.23%67.8% 89.00%100.00%44.44%";
    String[] splited = str.split("\\s+");
Vignesh T I
  • 782
  • 1
  • 7
  • 22
0

You could simply use String.split(" ") as follows:

String combinedString = "10.15%34.23%67.8% 89.00%100.00%44.44%";
String firstString = combinedString.split(" ")[0];
String secondString  = combinedString.split(" ")[1];

System.out.println(firstString);
System.out.println(secondString);

Output would look like this:

10.15%34.23%67.8%
89.00%100.00%44.44%
Ilya Maier
  • 475
  • 3
  • 12