C# String Split Method

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

 

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

 

C# String Split Method Representation Diagram

 

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

C# String Split Method Syntax

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

 

public string[] split(char[] separator)

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

C# String Split Method Example

Following is the example of splitting the given string with a comma (“,”) delimiter using the Split() method in the 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 Enter Key to Exit..");
           Console.ReadLine();
       }
    }
}

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

 

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

 

C# String Split Method Example Result

 

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

C# Split String with Multiple Delimiters

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

 

Following is the example of splitting the given string with multiple delimiters in the 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 Enter Key to Exit..");
          Console.ReadLine();
       }
    }
}

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

 

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

 

When you execute the above c# program, you will get the result below.

 

C# Split String with Multiple Delimiters Example Result

 

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

C# Split String into List

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

 

Following is the example to return the split method result as a list in the 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 list = new List(msg.Split(new char[] { ',', '-', '%' }, StringSplitOptions.RemoveEmptyEntries));
          for (int i = 0; i < list.Count; i++)
          {
             Console.WriteLine(list[i]);
          }
          Console.WriteLine("\nPress Enter Key to Exit..");
          Console.ReadLine();
       }
    }
}

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

 

When you execute the above c# program, you will get the result below.

 

C# Split String into List Example Result

 

This is how you can return the split method result as a list based on your requirements in the c# programming language.