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.
This will need to be cleaned up a bit, but this is essentially it....
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.
The signature is incorrect, you need to return a string array
WARNIG NEVER USED EXTENSIONs, so forgive me about some errors ;)
Personally I'd cheat and have a peek at string.Split using reflector...
InternalSplitOmitEmptyEntries
looks useful ;-)My first observation is that the separator ought to be a char not a string since escaping a string using a single character may be hard -- how much of the following string does the escape character cover? Other than that, @James Curran's answer is pretty much how I would handle it - though, as he says it needs some clean up. Initializing j to 0 in the loop initializer, for instance. Figuring out how to handle null inputs, etc.
You probably want to also support StringSplitOptions and specify whether empty string should be returned in the collection.