C# Extension Method - String Split that also accep

2019-01-24 10:47发布

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.

10条回答
贼婆χ
2楼-- · 2019-01-24 11:24

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();
查看更多
Viruses.
3楼-- · 2019-01-24 11:25
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;
}
查看更多
We Are One
4楼-- · 2019-01-24 11:26

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.

查看更多
▲ chillily
5楼-- · 2019-01-24 11:28

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;
}
查看更多
我只想做你的唯一
6楼-- · 2019-01-24 11:33

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

查看更多
淡お忘
7楼-- · 2019-01-24 11:41

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.

查看更多
登录 后发表回答