Question

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.

Was it helpful?

Solution

How about:

public static IEnumerable<string> Split(this string input, 
                                        string separator,
                                        char escapeCharacter)
{
    int startOfSegment = 0;
    int index = 0;
    while (index < input.Length)
    {
        index = input.IndexOf(separator, index);
        if (index > 0 && input[index-1] == escapeCharacter)
        {
            index += separator.Length;
            continue;
        }
        if (index == -1)
        {
            break;
        }
        yield return input.Substring(startOfSegment, index-startOfSegment);
        index += separator.Length;
        startOfSegment = index;
    }
    yield return input.Substring(startOfSegment);
}

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.

OTHER TIPS

This will need to be cleaned up a bit, but this is essentially it....

List<string> output = new List<string>();
for(int i=0; i<input.length; ++i)
{
    if (input[i] == separator && (i==0 || input[i-1] != escapeChar))
    {
        output.Add(input.substring(j, i-j);
        j=i;
    }
}

return output.ToArray();

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.

Here is solution if you want to remove the escape character.

public static IEnumerable<string> Split(this string input, 
                                        string separator, 
                                        char escapeCharacter) {
    string[] splitted = input.Split(new[] { separator });
    StringBuilder sb = null;

    foreach (string subString in splitted) {
        if (subString.EndsWith(escapeCharacter.ToString())) {
            if (sb == null)
                sb = new StringBuilder();
            sb.Append(subString, 0, subString.Length - 1);
        } else {
            if (sb == null)
                yield return subString;
            else {
                sb.Append(subString);
                yield return sb.ToString();
                sb = null;
            }
        }
    }
    if (sb != null)
        yield return sb.ToString();
}

You can try something like this. Although, I would suggest implementing with unsafe code for performance critical tasks.

public static class StringExtensions
{
    public static string[] Split(this string text, char escapeChar, params char[] seperator)
    {
        return Split(text, escapeChar, seperator, int.MaxValue, StringSplitOptions.None);
    }

    public static string[] Split(this string text, char escapeChar, char[] seperator, int count)
    {
        return Split(text, escapeChar, seperator, count, StringSplitOptions.None);
    }

    public static string[] Split(this string text, char escapeChar, char[] seperator, StringSplitOptions options)
    {
        return Split(text, escapeChar, seperator, int.MaxValue, options);
    }

    public static string[] Split(this string text, char escapeChar, char[] seperator, int count, StringSplitOptions options)
    {
        if (text == null)
        {
            throw new ArgumentNullException("text");
        }

        if (text.Length == 0)
        {
            return new string[0];
        }

        var segments = new List<string>();

        bool previousCharIsEscape = false;
        var segment = new StringBuilder();

        for (int i = 0; i < text.Length; i++)
        {
            if (previousCharIsEscape)
            {
                previousCharIsEscape = false;

                if (seperator.Contains(text[i]))
                {
                    // Drop the escape character when it escapes a seperator character.
                    segment.Append(text[i]);
                    continue;
                }

                // Retain the escape character when it escapes any other character.
                segment.Append(escapeChar);
                segment.Append(text[i]);
                continue;
            }

            if (text[i] == escapeChar)
            {
                previousCharIsEscape = true;
                continue;
            }

            if (seperator.Contains(text[i]))
            {
                if (options != StringSplitOptions.RemoveEmptyEntries || segment.Length != 0)
                {
                    // Only add empty segments when options allow.
                    segments.Add(segment.ToString());
                }

                segment = new StringBuilder();
                continue;
            }

            segment.Append(text[i]);
        }

        if (options != StringSplitOptions.RemoveEmptyEntries || segment.Length != 0)
        {
            // Only add empty segments when options allow.
            segments.Add(segment.ToString());
        }

        return segments.ToArray();
    }
}
public static string[] Split(this string input, string separator, char escapeCharacter)
{
    Guid g = Guid.NewGuid();
    input = input.Replace(escapeCharacter.ToString() + separator, g.ToString());
    string[] result = input.Split(new string []{separator}, StringSplitOptions.None);
    for (int i = 0; i < result.Length; i++)
    {
        result[i] = result[i].Replace(g.ToString(), escapeCharacter.ToString() + separator);
    }

    return result;
}

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.

The signature is incorrect, you need to return a string array

WARNIG NEVER USED EXTENSIONs, so forgive me about some errors ;)

public static List<String> Split(this string input, string separator, char escapeCharacter)
{
    String word = "";
    List<String> result = new List<string>();
    for (int i = 0; i < input.Length; i++)
    {
//can also use switch
        if (input[i] == escapeCharacter)
        {
            break;
        }
        else if (input[i] == separator)
        {
            result.Add(word);
            word = "";
        }
        else
        {
            word += input[i];    
        }
    }
    return result;
}

Personally I'd cheat and have a peek at string.Split using reflector... InternalSplitOmitEmptyEntries looks useful ;-)

I had this problem as well and didn't find a solution. So I wrote such a method myself:

    public static IEnumerable<string> Split(
        this string text, 
        char separator, 
        char escapeCharacter)
    {
        var builder = new StringBuilder(text.Length);

        bool escaped = false;
        foreach (var ch in text)
        {
            if (separator == ch && !escaped)
            {
                yield return builder.ToString();
                builder.Clear();
            }
            else
            {
                // separator is removed, escape characters are kept
                builder.Append(ch);
            }
            // set escaped for next cycle, 
            // or reset unless escape character is escaped.
            escaped = escapeCharacter == ch && !escaped;
        }
        yield return builder.ToString();
    }

It goes in combination with Escape and Unescape, which escapes the separator and escape character and removes escape characters again:

    public static string Escape(this string text, string controlChars, char escapeCharacter)
    {
        var builder = new StringBuilder(text.Length + 3);
        foreach (var ch in text)
        {
            if (controlChars.Contains(ch))
            {
                builder.Append(escapeCharacter);
            }
            builder.Append(ch);
        }
        return builder.ToString();
    }

    public static string Unescape(string text, char escapeCharacter)
    {
        var builder = new StringBuilder(text.Length);
        bool escaped = false;
        foreach (var ch in text)
        {
            escaped = escapeCharacter == ch && !escaped;
            if (!escaped)
            {
                builder.Append(ch);
            }
        }
        return builder.ToString();
    }

Examples for escape / unescape

separator = ','
escapeCharacter = '\\'
//controlCharacters is always separator + escapeCharacter

@"AB,CD\EF\," <=> @"AB\,CD\\EF\\\,"

Split:

@"AB,CD\,EF\\,GH\\\,IJ" => [@"AB", @"CD\,EF\\", @"GH\\\,IJ"]

So to use it, Escape before Join, and Unescape after Split.

public string RemoveMultipleDelimiters(string sSingleLine)
{
    string sMultipleDelimitersLine = "";
    string sMultipleDelimitersLine1 = "";
    int iDelimeterPosition = -1;
    iDelimeterPosition = sSingleLine.IndexOf('>');
    iDelimeterPosition = sSingleLine.IndexOf('>', iDelimeterPosition + 1);
    if (iDelimeterPosition > -1)
    {
        sMultipleDelimitersLine = sSingleLine.Substring(0, iDelimeterPosition - 1);
        sMultipleDelimitersLine1 = sSingleLine.Substring(sSingleLine.IndexOf('>', iDelimeterPosition) - 1);
        sMultipleDelimitersLine1 = sMultipleDelimitersLine1.Replace('>', '*');
        sSingleLine = sMultipleDelimitersLine + sMultipleDelimitersLine1;
    }
    return sSingleLine;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top