25

I've been developing an Android app for the past 4 Months now and came across the following regarding the split function:

String [] arr;
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
arr = result.toString().trim().split("|");

The result variable is what I get after accessing my WebService, now this works perfectly. But, for some reason my split("|") method is not splitting at "|" but rather splitting at every single char in my result String. So my array looks like this:

arr[0] is "H", arr[1] is "e", etc......

I don't know why this is happening because I have used it before in the same project and it worked perfectly.

Thank you in advance

Jannik
  • 332
  • 1
  • 3
  • 12

2 Answers2

72
arr = result.toString().trim().split("\\|");

the param of String.split accept a regular expression.

Changwei Yao
  • 13,051
  • 3
  • 25
  • 22
17

Following code can be used for any pattern splitting.

String.split(Pattern.quote("any pattern you would like here !"));
user1306828
  • 715
  • 7
  • 14