C # 's String intercept--split

Source: Internet
Author: User

The Previous blog, "C # string interception--substring" introduced the Substring function, while the implementation of the "Institution name/Teacher Name/course Type/Course name" in the name of the organization, teacher name, course type, the course name respectively intercepted. Today we will introduce a string intercept function split.


String.Split method: The returned string array contains the substrings in this instance, separated by the elements of the specified Unicode character array (separator).


There are six types of overloaded methods for the String.Split method, but I think they are all the same, but the parameters are different, but the requirements are different, the main parameters are:

1.char[] is delimited by the elements of the specified array of Unicode characters.
2.INT32 Specifies the maximum number of substrings to return.
3.StringSplitOptions This parameter is a parameter of an enumeration type, There are only two options: The first stringsplitoptions.none indicates that the return value includes an array element with an empty string, and the second stringsplitoptions.removeemptyentries indicates that the return value does not include an array element that contains an empty string.
4.string[] is delimited by the elements of the specified string array.


As long as we understand what the above four parameters mean, we can choose which split function we need to overload, depending on our needs. Here is a small example of the implementation of all of the Split function overloading methods:

Public string[] Split (params char[] separator);

Class program    {        static void Main (string[] args)        {            string s1 = "Institution name/Teacher Name/course Type/course name";            string[] split = S1. Split (new char[] {'/'});    Returns an array of substrings delimited by '/',            foreach (string s in Split)            {                Console.WriteLine (s);            }            Console.WriteLine ();        }    }



Public string[] Split (char[] separator, int count);

Class program    {        static void Main (string[] args)        {            string s1 = "Institution name/Teacher Name/course Type/course name";            string[] split = S1. Split (new char[] {'/'}, 3);   Returns an array of substrings separated by '/', with a maximum number of substrings of 3            foreach (string s in Split)            {                Console.WriteLine (s);            }            Console.WriteLine ();        }    }




Public string[] Split (char[] separator, stringsplitoptions options);

Class program    {        static void Main (string[] args)        {            string s1 = "Institution name/Teacher Name/course Type/course name";            Returns an array of substrings delimited by '/', stringsplitoptions.removeemptyentries means that the return value does not include an array element containing an empty string            string[] split = S1. Split (new char[] {'/'}, stringsplitoptions.removeemptyentries);            foreach (string s in Split)            {                Console.WriteLine (s);            }            Console.WriteLine ();        }    }




Public string[] Split (string[] separator, stringsplitoptions options);

Class program    {        static void Main (string[] args)        {            string s1 = "Institution name/Teacher Name/course Type/course name";            Returns an array of substrings separated by "name", stringsplitoptions.none means that the return value includes an array element containing an empty string            string[] A = new string[] {"Name"};            string[] split = S1. Split (A, stringsplitoptions.none);            foreach (string s in Split)            {                Console.WriteLine (s);            }            Console.WriteLine ();        }    }




Public string[] Split (char[] separator, int count, stringsplitoptions options);

Class program    {        static void Main (string[] args)        {            string s1 = "Institution name/Teacher Name/course Type/course name";            Returns an array of substrings delimited by '/', the maximum number of substrings is 3, stringsplitoptions.removeemptyentries means that the return value does not include an array element containing an empty string            string[] split = S1. Split (new char[] {'/'}, 3, stringsplitoptions.removeemptyentries);            foreach (string s in Split)            {                Console.WriteLine (s);            }            Console.WriteLine ();        }    }




Public string[] Split (string[] separator, int count, stringsplitoptions options);

Class program    {        static void Main (string[] args)        {            string s1 = "Institution name/Teacher Name/course Type/course name";            Returns an array of substrings separated by "first name", the maximum number of substrings is 3, stringsplitoptions.none means that the return value includes an array element containing an empty string            string[] A = new string[] {"Name"};            string[] split = S1. Split (new char[] {'/'}, 3, stringsplitoptions.none);            foreach (string s in Split)            {                Console.WriteLine (s);            }            Console.WriteLine ();        }    }




If we now want to implement the requirements in the previous blog: the string "Name of institution/Teacher name/course Type/Course name" will be intercepted separately according to "/". Isn't it easier? Directly with the first simplest overloaded function can solve our problem ...

Class program    {        static void Main (string[] args)        {            string s1 = "Institution name/Teacher Name/course Type/course name";            string[] split = S1. Split (new char[] {'/'});    Returns an array of substrings delimited by '/',            foreach (string s in Split)            {                Console.WriteLine (s);            }            Console.WriteLine ();        }    }




The Split function also implements the string interception function we want, although it returns an array of strings, overloaded with more parameters, but it's easier to understand than the SUBSTRING function ...









C # 's String intercept--split

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.