You need to sign in to do that
Don't have an account?
String.split for backslash
I need to split a string using the backslash as a delimeter. Here is the code:
public String removePath(String filename) { if (filename == null) return null; List<String> parts = filename.split('\\'); filename = parts[parts.size()-1]; return filename; } static testMethod void testRemovePath() { System.assertEquals('PPDSF100111.csv', EmailUtilities.getInstance().removePath('e:\\processed\\PPDSF100111.csv')); }
When you run the unit test, it errors on the line with split saying "System.StringException: Invalid regex: Unexpected internal error near index 1...".
If I try to use one or three backslashes, it returns a compile error: "Save error: Line breaks not allowed in literal strings."
The force.com documentation for String.split references the Java 5 documentation for regular expression. You are supposed to use two backslashes to effectively escape it to a literal backslash. Is this a bug? What is the workaround if it is?
Erik
I believe you need to do 4 backslashes, each one of the 2 you are trying to represent needs to be escaped.
public String removePath(String filename) { if (filename == null) return null; List<String> parts = filename.split('\\\\'); filename = parts[parts.size()-1]; return filename; } static testMethod void testRemovePath() { System.assertEquals('PPDSF100111.csv', EmailUtilities.getInstance().removePath('e:\\processed\\PPDSF100111.csv')); }