Given this string:
String number = "1234*987-654";
I want split and store in string array like [1234, *, 987, -, 654]
How I achieve this?
Given this string:
String number = "1234*987-654";
I want split and store in string array like [1234, *, 987, -, 654]
How I achieve this?
You could try splitting using lookarounds:
(?<=\D)(?=\d)|(?<=\d)(?=\D)
Sample script:
String number = "1234*987-654";
String[] parts = number.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
System.out.println(Arrays.toString(parts));
// [1234, *, 987, -, 654]
The logic here it split at the interface between any non digit and digit character, or vice-versa. Note that because we split using purely lookarounds, no characters actually get consumed.