find and solve || findandsolve.com
Please wait.....

Split String In C#

In c#, the String.Split()  method is used to split a string value into substrings based on the array's characters. The split method will return a string array that contains a substring that is the basis of characters in an array.


String.Split() method


public string[] Split(params Char[] ch)  Splits the params string value into substrings that are based on the characters in an array.

 

public string[] Split(Char[], Int32) 

Splits a string into a maximum number of substrings based on the characters in an array and This is return  maximum number of substrings.

[ComVisibleAttribute(false)]

public string[] Split(Char[], Int32, StringSplitOptions)

Splits a string into a maximum number of substrings based on the characters in an array.
[ComVisibleAttribute(false)]
public string[] Split(Char[], StringSplitOptions)  
Splits a string into substrings based on the characters in an array. You can specify whether the substrings include empty array elements.
[ComVisibleAttribute(false)]
public string[] Split(String[], Int32, StringSplitOptions)  
Splits a string into a maximum number of substrings based on the characters in an array.
public string[] Split(String[], StringSplitOptions)  Splits a string into substrings based on the strings in an array. You can specify whether the substrings include empty array elements.


Split String Into an Array

The simplest form of string split is splitting a string into an array of substrings separated by a character such as a comma. Listing 1 is the code example that has a string of name separated by a comma and a space. The name.Split method splits the string into an array of name that are separated by a comma and space. 

 Example
   using System;
    public class StringExample
    {
        public static void Main(string[] args)
        {
          Console.WriteLine("Comma separated strings");
         // String of Name 
          string name= "Sylvia, Debin , Sukalewa, Prakhatiwa";
         // Split name separated by a comma followed by space
          string[] nameList = name.Split(", ");
          foreach (string name in authorsList)
         Console.WriteLine(name);  
         }
      }  


output
Sylvia
Debin
Sukalewa
Prakhatiwa


C# Split String with Multiple Delimiters

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

Example
using System;
namespace MultipleDelimiters
{
    class Program
    {
       static void Main(string[] args)
       {
          string name= "Sylvia,Rohit,-Prasai%Lamike";
          string[] nameList= name.Split(new char[] { ',', '-', '%' }, StringSplitOptions.RemoveEmptyEntries);
          for (int i = 0; i < nameList.Length; i++)
          {
              Console.WriteLine(nameList[i]);
          }
          Console.WriteLine("\nPress Enter Key to Exit..");
          Console.ReadLine();
       }
    }
}
output
Sylvia
Rohit
Prasai
Lamike


C# Split Example

We could use the IndexOf method to get a location of the string, and split it using that position, and the length of the search string

using System;
using System.Text.RegularExpressions;
public class Program
{
 public static void Main()
 {
     string value = "red\r\ngreen\r\nblack\r\nyellow";
    // Split the string on line breaks.
    // ... The return value from Split is a string[] array.
    string[] lines = Regex.Split(value, "\r\n");
    foreach (string line in lines) {
        Console.WriteLine(line);
    }
 }
}

output
red
green
black
yellow