String truncation of C # -- Split,

Source: Internet
Author: User

String truncation of C # -- Split,
In the previous blog "string truncation of C # -- Substring", we introduced the Substring function, and implemented the name of the institution in "institution name/instructor name/course type/Course name, the instructor name, course type, and course name are separated. Today, we will introduce a string truncation function Split.


String. Split method: the returned String array contains the sub-String in this instance (separated by elements of the specified Unicode character array (separator ).


The String. Split method has six types of overload methods, but I think they are all similar. They only have different parameters but different requirements. The main parameters include:

1. Char [] is separated by elements in the specified array of Unicode characters.
2. Int32 indicates the maximum number of returned substrings.
3. stringSplitOptions is an enumeration type parameter. There are only two options: the first StringSplitOptions. none indicates that the returned value includes an array element containing an empty string. The second type is StringSplitOptions. removeEmptyEntries indicates that the returned value does not include array elements containing null strings.
4. String [] is separated by 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 reload based on our needs. The following is a small example of how to implement the methods of overloading all Split functions:

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

Class Program {static void Main (string [] args) {string s1 = "institution name/instructor name/course type/Course name "; string [] split = s1.Split (new char [] {'/'}); // returns the substring array 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/instructor name/course type/Course name "; string [] split = s1.Split (new char [] {'/'}, 3); // returns an array of substrings separated, the maximum number of substrings is 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/instructor name/course type/Course name "; // return the substring array separated by '/', StringSplitOptions. removeEmptyEntries indicates that the returned value does not include the 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/instructor name/course type/Course name "; // returns an array of substrings separated by "names", StringSplitOptions. none indicates that the returned value includes the 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/instructor name/course type/Course name "; // returns an array of substrings separated by '/'. The maximum number of substrings is 3, and StringSplitOptions is returned. removeEmptyEntries indicates that the returned value does not include the 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/instructor name/course type/Course name "; // returns an array of substrings separated by "names". The maximum number of substrings is 3, and StringSplitOptions is returned. none indicates that the returned value includes the array element 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 want to achieve the requirement in the previous blog: extract the string "institution name/instructor name/course type/Course name". Is it simpler? Using the first simplest overload function can solve our problem...

Class Program {static void Main (string [] args) {string s1 = "institution name/instructor name/course type/Course name "; string [] split = s1.Split (new char [] {'/'}); // returns the substring array foreach (string s in split) {Console. writeLine (s);} Console. writeLine ();}}




The Split function can also implement the string truncation function we want. Although it returns a string array, there are many overloaded parameters, but it is easier to understand than Substring functions ......









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.