I’d like to write an extension method for the .NET String class. I’d like it to be a special varation on the Split method – one that takes an escape character to prevent splitting the string when a escape character is used before the separator.
What’s the best way to write this? I’m curious about the best non-regex way to approach it.
Something with a signature like…
public static string[] Split(this string input, string separator, char escapeCharacter) { // ... }
UPDATE: Because it came up in one the comments, the escaping…
In C# when escaping non-special characters you get the error – CS1009: Unrecognized escape sequence.
In IE JScript the escape characters are throw out. Unless you try \u and then you get a ‘Expected hexadecimal digit’ error. I tested Firefox and it has the same behavior.
I’d like this method to be pretty forgiving and follow the JavaScript model. If you escape on a non-separator it should just ‘kindly’ remove the escape character.
How about:
That seems to work (with a few quick test strings), but it doesn’t remove the escape character – that will depend on your exact situation, I suspect.