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

Split() Method in C# String

In the C#, Split is used to break a delimited string into substrings. It method creates an array of substrings by splitting the input string based on one or more delimiters. In the blog we are  discuss various forms of String.Split method and how to split strings using different delimiters in C#  programming language.

Syntax

public string[] Split(params Char[] ch)  
public string[] Split(Char[], Int32)
[ComVisibleAttribute(false)]
public string[] Split(Char[], Int32, StringSplitOptions)
[ComVisibleAttribute(false)]
public string[] Split(Char[], StringSplitOptions)
[ComVisibleAttribute(false)]
public string[] Split(String[], Int32, StringSplitOptions)
public string[] Split(String[], StringSplitOptions)  
Parameter

ch: it is a character type array.

Return

It returns array of string


MethodDescription
Split(String[], Int32, StringSplitOptions)Splits a string into a maximum number of substrings based on the strings in an array. You can specify whether the substrings include empty array elements.
Split(Char[], Int32, StringSplitOptions)Splits a string into a maximum number of substrings based on the characters in an array.
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(Char[])Splits a string into substrings that are based on the characters in an array
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.
Split(Char[], Int32)Splits a string into a maximum number of substrings based on the characters in an array. You also specify the maximum number of substrings to return.

C# String Split() Method Example

using System;
public class Program
{
 public static void Main()
 {
   string str = "Hello Find And Solve";
           string[] str1 = str.Split(' ');
           foreach (string item in str1)
           {             Console.WriteLine(item);
           }
  } }

Output

Hello

Find

And

Solve

 Split(Char[], Int32, StringSplitOptions) Method

using System;
public class Program
{
 public static void Main()
 {
   // Taking a string
        String str = "Find, And Solve";
        char[] spearator = { ',', ' ' };
        Int32 count = 2;
        // Using the Method
        String[] strlist = str.Split(spearator,
               count, StringSplitOptions.None);
        foreach(String s in strlist)
        {
            Console.WriteLine(s);
        }  }
}

Output 

Find

   And Solve

Split(String[], StringSplitOptions) Method

Syntax

public String[] Split(String[] separator, StringSplitOptions options);

Example

using System;
public class Program
{
 public static void Main()
 {
  // Taking a string
        String str = "Find, And Solve";
        String[] spearator = { "d,", "And" };
        // using the method
        String[] strlist = str.Split(spearator,
           StringSplitOptions.RemoveEmptyEntries);
        foreach(String s in strlist)
        {
            Console.WriteLine(s);
        }
 }
}

Output

Fin

   Solve