In this post we’ll learn about String.Split() Method. This is common method used with String class and returns an array of strings. In this method array is generated by splitting of original string separated by the delimiters passed as a parameter in Split() method. Delimiters can be single value or array of characters. 

Split String into an Array using Single character/Delimiter

In below example we have taken one Delimiters (, ) which is comma. Given string includes a plain text of student names with comma separated and String.Split() will return an array of string which include all Student names in it.

        static void Main(string[] args)
        {
            // String of Student names

            string Student = "Rakesh, Mukesh, Amit, Henry, Chris, Ken, Joe, Raj, Dev, Praveen";
            // Split Student separated by a comma followed by space
            string[] StudentList = Student.Split(',');
            foreach (string studentName in StudentList)
            {
                Console.WriteLine(studentName);
            }
            Console.ReadLine();
        }

Output of String.Split() with single delimiter

String.Split() Method

String.Split() using multiple characters/delimiter

In above example we delt with one delimiter. String.Split() method can also separate strings based on multiple characters/delimiters using the same method. The String.Split() method will accept an argument of an array of characters and splits the string based on the characters present in the array.

        static void Main(string[] args)
        {
            // String of Student names

            string Student = "Rakesh, Mukesh kumar, Amit-Singh, Henry , Chris, Ken, Joe , Raj, Dev, Praveen";
            // Split Student string using multiple delimiters
            string[] StudentList = Student.Split(new Char[] { ' ', '-'});
            foreach (string studentName in StudentList)
            {
                Console.WriteLine(studentName);
            }
            Console.ReadLine();
        }

 

Output of String.Split() with multiple delimiters

String.Split() Method

Split(Char[], Int32) method

Above method is used with string and it split string into maximum number of sub strings based on the array of characters passed as parameter. Additionally, we can also specify maximum number of sub strings to return.

Split(Char[], Int32, StringSplitOptions) method

Above method is used with string and it split string into maximum number of sub strings based on the array of characters passed as parameter. One extra feature of this methos is that you can specify whether to include the empty array elements in array of sub strings or not.

You may like

Explore More

Working With File Class In C#

In C#, ‘File’ class provides many static methods for moving, copying, and deleting files. These Static methods involve moving a file and copying and deleting a file also. Let’s check

How to Read a Text File in C#

In C# ‘File’ class provides static methods to read given text file data. The File.ReadAllText() method opens a text file, reads all the text present in the file into a

C# | How to use Interface references

In C#, you’re permitted to make a reference variable of an interface type or in other words, you’re permitted to form an interface reference variable. Such kind of variable can