0

I have the following string "ABC" and "AAA||BBB"

I am trying to split it using the characters "||" but the split method is taking this as a regex expression, returning an array of characters instead of {"ABC"} and {"AAA", "BBB"}

I have tried scaping the bar with a back slash, but that didn't work.

How can I make the split method to take "||" as a String and not as a regex?

Thanks

marimaf
  • 5,019
  • 3
  • 46
  • 61
  • possible duplicate of [String.split() \*not\* on regular expression?](http://stackoverflow.com/questions/6374050/string-split-not-on-regular-expression) – Eric Apr 30 '13 at 18:17

3 Answers3

5

Escape the pipes

Use \\|\\| instead

FDinoff
  • 28,493
  • 5
  • 67
  • 88
4

If you don't want to deal with escaping then you can use Pattern#quote:

String[] tok = "AAA||BBB".split(Pattern.quote("||"));

OR simple:

String[] tok = "AAA||BBB".split("\\Q||\\E"));
anubhava
  • 664,788
  • 59
  • 469
  • 547
0
   String[] result = "The||man is very happy.".split("\\|\\|");

    for (int x=0; x<result.length; x++){

        System.out.print(result[x]);
     }

There you go its simple

Junaid Hassan
  • 634
  • 1
  • 11
  • 31