C# String.Split Usage Quickly Master

The String.Split method is used to split the original string according to the specified string, and returns the string array obtained after splitting.

The grammar of String.Split includes the following 8 types, which can be divided into two categories according to whether the separator is a character (char) or a string (string). The first and second usages of these two categories are included in the third and fourth usages. Usage (see 1b, 1d, 2b, 2d in the sample code cited).

//第一类:字符char作为分隔符

1. Split(Char[])  //根据指定的分隔字符将字符串拆分为子字符串。

2. Split(Char[], Int32)  //根据指定的分隔字符将一个字符串拆分成最大数量的子字符串。

3. Split(Char[], StringSplitOptions)  //根据指定的分隔字符和选项将字符串拆分为子字符串。

4. Split(Char[], Int32, StringSplitOptions)  //基于指定的分隔字符和(可选)选项将字符串拆分为最大数量的子字符串。

/*——————————————————————————————————————————————————————————————————————————*/

//第二类:字符串string作为分隔符

1. Split(String[])  //根据指定的分隔字符串将字符串拆分为子字符串。

2. Split(String[], Int32)  //根据指定的分隔字符串将一个字符串拆分成最大数量的子字符串。

3. Split(String[], StringSplitOptions)  //基于指定的分隔字符串和(可选)选项将字符串拆分为子字符串。

4. Split(String[], Int32, StringSplitOptions)  //基于指定的分隔字符串和(可选)选项将字符串拆分为最大数量的子字符串。

The best way to understand is to learn the code. Here is an example, including the use of the various methods mentioned above.

code show as below. The first and second usages of the two types are included in the third and fourth usages (that is, 1b, 1d, 2b, 2d in the following example code)

    class Program
    {
        static void Main(string[] args)
        {
            string s1 = ",ONE,,TWO,,,THREE,,";
            string s2 = "[stop]" +
                        "ONE[stop][stop]" +
                        "TWO[stop][stop][stop]" +
                        "THREE[stop][stop]";
            char[] charSeparators = new char[] { ',' };
            string[] stringSeparators = new string[] { "[stop]" };
            string[] result;
            // ------------------------------------------------------------------------------
            // 第一类:字符类型“char”作为分隔符的用法
            // ------------------------------------------------------------------------------
            Console.WriteLine("1) Split a string delimited by characters:\n");

            Console.WriteLine("1a) The original string is \"{0}\".", s1);  //1a) The original string is ",ONE,,TWO,,,THREE,,".
            Console.WriteLine($"The delimiter character is '{charSeparators[0]}'.\n");  //The delimiter character is ','.

            Console.WriteLine("1b) Split a string delimited by characters and " +
                              "return all elements:");   //1b) Split a string delimited by characters and return all elements:
            result = s1.Split(charSeparators, StringSplitOptions.None);
            Show(result);  //<><ONE><><TWO><><><THREE><><>

            Console.WriteLine("1c) Split a string delimited by characters and " +
                              "return all non-empty elements:");   //1c) Split a string delimited by characters and return all non-empty elements:
            result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
            Show(result);  //<ONE><TWO><THREE>

            Console.WriteLine("1d) Split a string delimited by characters and " +
                              "return 2 elements:");
            result = s1.Split(charSeparators, 2, StringSplitOptions.None);
            Show(result);  //<><ONE,,TWO,,,THREE,,>

            Console.WriteLine("1e) Split a string delimited by characters and " +
                              "return 2 non-empty elements:");
            result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
            Show(result);  //<ONE><TWO,,,THREE,,>

            // ------------------------------------------------------------------------------
            // 第二类:字符串类型“string作为分隔符的用法
            // ------------------------------------------------------------------------------
            Console.WriteLine("2) Split a string delimited by another string:\n");

            Console.WriteLine($"2a) The original string is \"{s2}\".");
            Console.WriteLine("The delimiter string is \"{0}\".\n", stringSeparators[0]);  //The delimiter string is "[stop]".

            Console.WriteLine("2b) Split a string delimited by another string and " +
                              "return all elements:");
            result = s2.Split(stringSeparators, StringSplitOptions.None);
            Show(result);  //<><ONE><><TWO><><><THREE><><>

            Console.WriteLine("2c) Split a string delimited by another string and " +
                              "return all non-empty elements:");
            result = s2.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
            Show(result);  //<ONE><TWO><THREE>

            Console.WriteLine("2d) Split a string delimited by another string and " +
                              "return 2 elements:");
            result = s2.Split(stringSeparators, 2, StringSplitOptions.None);
            Show(result);  //<><ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]>

            Console.WriteLine("2e) Split a string delimited by another string and " +
                              "return 2 non-empty elements:");
            result = s2.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
            Show(result);  //<ONE><TWO[stop][stop][stop]THREE[stop][stop]>

            void Show(string[] entries)
            {
                Console.WriteLine($"The return value contains these {entries.Length} elements:");
                foreach (string entry in entries)
                {
                    Console.Write($"<{entry}>");
                }
                Console.Write("\n\n");
            }         
        }
    }

The complete running result is as follows:

Guess you like

Origin blog.csdn.net/Dust_Evc/article/details/114991524