Reputation: 540
i need to split a String into parts of number sequences and chars between them. Something like this:
input: "123+34/123(23*12)/100"
output[]:["123","+","34","/","123","(","23","*","12",")","/","100"]
Is this somehow possible, or is it possible to split a String by multiple chars? Otherwise, is it possible to loop through a String in Java?
Upvotes: 1
Views: 92
Reputation: 174696
Use a lookahead assertion based regex for splitting the input string.
String input = "123+34/123(23*12)/100";
System.out.println(Arrays.toString(input.split("(?<=[/)+*])\\B(?=[/)+*])|\\b")));
Output:
[123, +, 34, /, 123, (, 23, *, 12, ), /, 100]
Upvotes: 0
Reputation: 72844
You can use a regular expression.
String input = "123+34/123(23*12)/100";
Pattern pattern = Pattern.compile("\\d+|[\\+\\-\\/\\*\\(\\)]");
Matcher matcher = pattern.matcher(input);
while(matcher.find()) {
System.out.println(matcher.group());
}
Upvotes: 3