Split a string by another string in C#

后端 未结 10 1687
小蘑菇
小蘑菇 2020-11-22 10:17

I\'ve been using the Split() method to split strings, but this only appears to work if you are splitting a string by a character. Is there a way to split a

10条回答
  •  天命终不由人
    2020-11-22 11:08

    The previous answers are all correct. I go one step further and make C# work for me by defining an extension method on String:

    public static class Extensions
    {
        public static string[] Split(this string toSplit, string splitOn) {
            return toSplit.Split(new string[] { splitOn }, StringSplitOptions.None);
        }
    }
    

    That way I can call it on any string in the simple way I naively expected the first time I tried to accomplish this:

    "a big long string with stuff to split on".Split("g str");
    

提交回复
热议问题