function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JPClark3JPClark3 

String.Split doesn't work with vertical bar character "|" [and other regex characters]

This is pretty strange. When spliting a string using a vertical bar, it appears to split between numerical characters also.

String.Split actually uses regular expressions to perform the split. So even though I'm using the same character, it is interpreted differently. This will be the case for other regeular expression characters also.

 

So the solution to this issue is to use two delimiters, one to join and one to split:

string DelJoin = '|';

string DelSplit = '\\|';

 

 

 

//Original test code:
public static testmethod void SplitTest() { string delim = '|'; string ToSplit = '-9.99999999911101E14' + delim + '1000011'; List<string> SplitValues = ToSplit.split(delim); System.Debug('@@@SplitTest:' + SplitValues); integer sz = SplitValues.Size(); System.AssertEquals(2, sz); //This fails with a value of 29 integer Pointer = integer.valueOf(SplitValues[sz-1]); System.Debug('@@@SplitTest:' + Pointer); System.AssertEquals(1000011, Pointer); }

 

 

Best Answer chosen by Admin (Salesforce Developers) 
JPClark3JPClark3

Yes, that's how I came to the conclusion that is was using RegEx in the split. That feature will come in handy some day.

I've determined, for this and going forward, I will use an unprintable ASCII character,

The 1F (hex) character is specificall for "Unit Separater". This has no special meaning in regular expressions.

 

private static final string DELIMITER =  '&#31;';

 


 

All Answers

spraetzspraetz

This appears to either be something VERY unique about String.split() and the pipe character |

 

Will look into it.

spraetzspraetz

I found a blog entry which explains it pretty well in Java.  In the case of String.split(), Apex follows the same rules as Java.

 

http://www.jamesnetherton.com/blog/2010/10/13/java-string-split-with-pipe-characters/

 

Hope this helps!

sfdcfoxsfdcfox

Just to add to spraetz's reply, you may wish to read the entire String.split() entry from the Java documentation, because it is literally identical behavior (in fact, the documentation for this function actually has a link to the corresponding Java documentation that covers this feature).

JPClark3JPClark3

Yes, that's how I came to the conclusion that is was using RegEx in the split. That feature will come in handy some day.

I've determined, for this and going forward, I will use an unprintable ASCII character,

The 1F (hex) character is specificall for "Unit Separater". This has no special meaning in regular expressions.

 

private static final string DELIMITER =  '&#31;';

 


 

This was selected as the best answer