Quantcast
Channel: Splitting a string in java on more than one symbol - Stack Overflow
Viewing all articles
Browse latest Browse all 5

Answer by Mena for Splitting a string in java on more than one symbol

$
0
0

String.split takes a regular expression as argument.

This means you can alternate whatever symbol or text abstraction in one parameter in order to split your String.

See documentation here.

Here's an example in your case:

String toSplit = "a+b-c*d/e=f";
String[] splitted = toSplit.split("[-+*/=]");
for (String split: splitted) {
    System.out.println(split);
}

Output:

a
b
c
d
e
f

Notes:

  • Reserved characters for Patterns must be double-escaped with \\. Edit: Not needed here.
  • The [] brackets in the pattern indicate a character class.
  • More on Patterns here.

Viewing all articles
Browse latest Browse all 5

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>