C# String Split Method

  Keywords : c# split string, c# string split example, c# split string by comma, c# string split to array, c# split string into list, csharp split, split c# example

Here we will learn string split method in c#, how to split string into array or list with split method and how to split string by comma or space in c# with examples.

C# String Split Method

In c#, the string Split method is used to split a string into substrings based on the characters in an array. The split method will return a string array which contains a substrings that are delimited by the specified characters in an array.

 

Following is the pictorial representation of split method functionality in c# programming language.

 

C# String Split Method Representation Diagram

 

If you observe above diagram, we are splitting the stringSuresh-Rohini-Trishika” with delimiter “-” using Split method. Once splitting is done, then the split method will return a string array like as shown above.

C# String Split Method Syntax

Following is the syntax of defining a split method in c# programming language.

 

public string[] split(char[] separator)

If you observe syntax, we are using character array (char[]) to define a delimiters to split the given string into substrings and return it as string array.

C# String Split Method Example

Following is the example of splitting the given string with comma (“,”) delimiter using Split() method in c# programming language.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            string msg = "Suresh,Rohini,Trishika";

            string[] strarr = msg.Split(',');

            for (int i = 0; i < strarr.Length; i++)

            {

                Console.WriteLine(strarr[i]);

            }

            Console.WriteLine("\nPress Any Key to Exit..");

            Console.ReadLine();

        }

    }

 

}

If you observe above example, we used a split() method to split the given string ("Suresh,Rohini,Trishika") with comma (',') delimiter and returning a substrings as string array. Here we used a for loop to iterate through string array to display array elements.

 

When we execute above c# program, we will get the result like as shown below.

 

C# String Split Method Example Result

 

This is how we can split a string with required delimiters using split() method in c# programming language.

C# Split String with Multiple Delimiters

In above example, we used only one comma delimiter with split() method to split the given string. Even we can use multiple delimiters with split() method to split the given string and we can remove an empty subscripts in string array.

 

Following is the example of splitting the given string with multiple delimiters in c# programming language.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            string msg = "Suresh,Rohini,Trishika,-Praveen%Sateesh";

            string[] strarr = msg.Split(new char[] { ',', '-', '%' }, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < strarr.Length; i++)

            {

                Console.WriteLine(strarr[i]);

            }

            Console.WriteLine("\nPress Any Key to Exit..");

            Console.ReadLine();

        }

    }

 

}

If you observe above example, we used a split() method with multiple delimiters to split the given string into string array.

 

Here the StringSplitOptions.RemoveEmptyEntries property is used to remove an empty string elements while returning the result array.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Split String with Multiple Delimiters Example Result

 

This is how we can split the given string with multiple delimiters using split() method in c# programming language.

C# Split String into List

Generally, the split method in c# will return a result as string array. In case, if we want to return a result as list, then we can convert a string array to list using List object.

 

Following is the example to return a split method result as list in c# programming language.

 

using System;

using System.Collections.Generic;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            string msg = "Suresh,Rohini,Trishika,-Praveen%Sateesh";

            IList<string> list = new List<string>(msg.Split(new char[] { ',', '-', '%' }, StringSplitOptions.RemoveEmptyEntries));

            for (int i = 0; i < list.Count; i++)

            {

                Console.WriteLine(list[i]);

            }

            Console.WriteLine("\nPress Any Key to Exit..");

            Console.ReadLine();

        }

    }

 

}

If you observe above example, we are converting a split method string array result as list using List object. We will learn more about List in next chapters.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Split String into List Example Result

 

This is how we can return split method result as list based on our requirements in c# programming language.