I have numbers and to want to split it to list of string using special character on it and with out removing special character on it like split
1,245.00 to 1 , 245 . 00
I have numbers and to want to split it to list of string using special character on it and with out removing special character on it like split
1,245.00 to 1 , 245 . 00
Split your string using lookaheads:
String input = "1,245.00";
String[] parts = input.split("(?=[^A-Za-z0-9])|(?<=[^A-Za-z0-9])");
for(String part : parts) {
System.out.println(part);
}
This splits if, at any position is the string, either the preceding or proceeding character be a non letter or number.
Output:
1
,
245
.
00
Demo here:
There are multiple options available. You can use the boundary (credits to @4castle), but also lookahead (credits to the previous reply) and lookbehind.
Here are three options which all work:
String input = "1,245.00";
// look-ahead only
Stream.of(input.split("(?=[,.])|(?<=[^\\d])")).forEach(System.out::println);
System.out.println();
// Boundary
Stream.of(input.split("\\b")).forEach(System.out::println);
System.out.println();
// Mix of look-behind and look-ahead
Stream.of(input.split("(?![\\d])|(?<=[^\\d])")).forEach(System.out::println);
All print together:
1
,
245
.
00
1
,
245
.
00
1
,
245
.
00