1

in my android app, i would like to split an array value into another array.

i have an Array with the name ArrayA. log of ArrayA[0]:

Peter|Mustermann

now i would like to split Peter and Mustermann, i try this:

String [] ArrayB = ArrayA[0].split("|");

But the Log of ArrayB[0] and ArrayB[1] will not be:

Peter

And

Mustermann

it will be:

P

And

nothing

Any ideas ? :(

SpecialFighter
  • 573
  • 1
  • 9
  • 14

3 Answers3

5

The public String[] split(String regex) works with a regular expression.

In a regular expression | is a reserved character, so try to escape it using

String [] ArrayB = ArrayA[0].split("\\|");

See here for more information about other reserved characters: http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#sum

And see here for a compilable sample: http://ideone.com/fjXNoJ

erg
  • 1,632
  • 1
  • 11
  • 23
1

You'll have use it as follows:

String [] ArrayB  = Array[0].trim().split("\\|");

As '\' is a special character also (along with |), two backslashes in a string literal will mean one backslash in the actual String, which in turn will represent a regular expression that matches a single backslash character.

0

Just use a single quote

String [] ArrayB = ArrayA[0].split('|');
Viral Patel
  • 32,418
  • 18
  • 82
  • 110