16

I want to split a string when following of the symbols encounter "+,-,*,/,=" I am using split function but this function can take only one argument.Moreover it is not working on "+". I am using following code:-

Stringname.split("Symbol");

Thanks.

Saumyaraj
  • 1,220
  • 3
  • 15
  • 37
  • Have you started by reading the javadoc? There must be some explanation as to why splitting on `+` doesn't give you what you want. – Sotirios Delimanolis Aug 05 '13 at 14:49
  • Maybe have a look at: http://stackoverflow.com/questions/7492672/java-string-split-by-multiple-character-delimiter – kadrian Aug 05 '13 at 14:51

4 Answers4

47

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.
Mena
  • 47,782
  • 11
  • 87
  • 106
  • @RohitJain yep, just realized. I've edited my answer, thanks for pointing out. – Mena Aug 05 '13 at 14:56
  • @saumyaraj I don't understand your question? – Mena Aug 05 '13 at 15:09
  • I mean that when i use "+" in split function it showed me error something like "Dangling meta character". Can u explain a bit about this error?Thanks – Saumyaraj Aug 05 '13 at 15:21
  • @saumyaraj yes. That is a meta-character, as in, a reserved character for a `Pattern`. It indicates a quantifier (see the [Pattern](http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html) page for more info). It'll work ok in a character class, but will need to be escaped if interpreted as literal "+" outside a character class. In your case, just try double escaping it as `\\+` instead of `+`. – Mena Aug 05 '13 at 15:39
10

You can use a regular expression:

String[] tokens = input.split("[+*/=-]");

Note: - should be placed in first or last position to make sure it is not considered as a range separator.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • 1
    `input.split("[+*-/=]");` also includes `,` and `.`. Just split `"a,b.c"` as an example, because the range from `*` to `/` includes those characters. – jlordo Aug 05 '13 at 15:11
  • `input.split("[+*/-=]");` even includes digits, the colon, semicolon and the `<` symbol. – jlordo Aug 05 '13 at 15:13
  • The dash inside a character class is always considered a range seperator, except if it's in the first or last position. – jlordo Aug 05 '13 at 15:17
6

You need Regular Expression. Addionaly you need the regex OR operator:

String[]tokens = Stringname.split("\\+|\\-|\\*|\\/|\\=");
Dennis Kriechel
  • 3,719
  • 14
  • 40
  • 62
1

For that, you need to use an appropriate regex statement. Most of the symbols you listed are reserved in regex, so you'll have to escape them with \.

A very baseline expression would be \+|\-|\\|\*|\=. Relatively easy to understand, each symbol you want is escaped with \, and each symbol is separated by the | (or) symbol. If, for example, you wanted to add ^ as well, all you would need to do is append |\^ to that statement.

For testing and quick expressions, I like to use www.regexpal.com

Deactivator2
  • 311
  • 1
  • 10