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.
Probably not the best way of doing it, but it's another alternative. Basically, everywhere the sequence of escape+seperator is found, replace it with a GUID (you can use any other random crap in here, doesn't matter). Then use the built in split function. Then replace the guid in each element of the array with the escape+seperator.
I had this problem as well and didn't find a solution. So I wrote such a method myself:
It goes in combination with Escape and Unescape, which escapes the separator and escape character and removes escape characters again:
Examples for escape / unescape
Split:
So to use it, Escape before Join, and Unescape after Split.
Here is solution if you want to remove the escape character.
You can try something like this. Although, I would suggest implementing with unsafe code for performance critical tasks.