[SalesForce] problem replacing and splitting string

I am trying to replace a pipe character used as a delimiter in a string. The string comes back with the pipe as the html entity code like this: Unassigned|Scheduled|Tech on Site

Now, I'm trying to replace and/or split this string into a string list, but I can't seem to get the replace or split to work.

string s = String.valueOf(params.get('status'));
string newString = s.replace('|', '|');
String[]  strs =  newString.split('|');

I get the following result:

strs (, U, n, a, s, s, i, g, n, e, ...)

Thanks for any help.

Best Answer

Split in Apex is very similar to Java string split.

the pattern you pass to the split method is not a direct match, its a REGEX pattern. so the pipe symbol | is considered as a Regex input and it splits the string into individual characters.

You can use the below code to get thru this

newString.split('\\|')

here's some other discussions around the same topic

https://stackoverflow.com/questions/6978901/splitting-a-string-using

and a blog post from Lacey on the same topic