I have text data and I want to split into String
and int
arrays Java
String text = "one1two20three300four4000";
Desired output:
String str[] = "one","two","three","four";
int num[] = 1,20,300,4000;
I have text data and I want to split into String
and int
arrays Java
String text = "one1two20three300four4000";
Desired output:
String str[] = "one","two","three","four";
int num[] = 1,20,300,4000;
This one will work for you in Java :
public static void main(String[] args) {
String text = "one1two20three300four4000";
String arr1[] = text.replaceFirst("^\\d+", "").split("\\d+");
String arr2[] = text.replaceFirst("^\\D+", "").split("\\D+");
System.out.println(Arrays.toString(arr1));
System.out.println(Arrays.toString(arr2)); // parse each value as an in using Streams (preferably) or a loop.
}
O/P :
[one, two, three, four]
[1, 20, 300, 4000]
PS : In java 8 Use int[] arr2Int = Arrays.stream(arr2).mapToInt(Integer::parseInt).toArray();
to convert your String array to int array.