C# Extension Method - String Split that also accepts an Escape Character

后端 未结 10 1729
我在风中等你
我在风中等你 2020-12-17 00:19

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 s

10条回答
  •  没有蜡笔的小新
    2020-12-17 00:56

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

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

    public static List Split(this string input, string separator, char escapeCharacter)
    {
        String word = "";
        List result = new List();
        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;
    }
    

提交回复
热议问题