String.Split() Method in C# with Examples

In C#, Split() is a string class method. The Split() method returns an array of strings generated by splitting of original string separated by the delimiters passed as a parameter in Split() method. The delimiters can be a character or an array of characters or an array of strings. Or you can also say that it returns a string array which contains the substrings in the current instance that are delimited by elements of a specified string or Unicode character array.
There are 6 methods in the overload list of this method as follows:

Method Description
Split(String[], Int32, StringSplitOptions) Split string into maximum number of sub strings based on the array of strings passed as parameter. You can specify whether to include the empty array elements in array of sub strings or not.
Split(Char[], Int32, StringSplitOptions) Split string into maximum number of sub strings based on the array of characters passed as parameter. You can specify whether to include the empty array elements in array of sub strings or not.
Split(String[], StringSplitOptions) Splits a string into substrings based on the array of strings. You can specify whether to include the empty array elements in array of sub strings or not.
Split(Char[]) Splits a string into substrings based on the array of characters.
Split(Char[], StringSplitOptions) Splits a string into substrings based on the array of characters. You can specify whether to include the empty array elements in array of sub strings or not.
Split(Char[], Int32) Split string into maximum number of sub strings based on the array of characters passed as parameter. You can specify maximum number of sub strings to return.

1. Split(String[], Int32, StringSplitOptions) Method

This method is used to 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.

Syntax:

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

Parameters:

  • separator: It is a string array which delimits the substrings in this string, an empty array that contains no delimiters, or null.
  • count: It is the maximum number of substring to return.
  • options: RemoveEmptyEntries option to omit empty array elements from the array returned or None option to include empty array elements in the array returned.

Return: This method returns an array whose elements contain the substrings in this string which are delimited by one or more characters in the separator.

Exceptions:

  • ArgumentOutOfRangeException: If the count is negative.
  • ArgumentException: If the options is not one of the StringSplitsOptions values.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the 
// Split(String[], Int32, StringSplitOptions)
// Method
using System;
  
class GFG {
  
    // Main Method    
    static void Main(string[] args)
    {
  
        // Taking a string
        String str = "Geeks, For Geeks";
  
        String[] spearator = { "s, ", "For" };
        Int32 count = 2;
  
        // using the method
        String[] strlist = str.Split(spearator, count,
               StringSplitOptions.RemoveEmptyEntries);
  
        foreach(String s in strlist)
        {
            Console.WriteLine(s);
        }
    }
}

chevron_right


Output:

Geek
 Geeks

2. Split(Char[], Int32, StringSplitOptions) Method

This method is used to splits a string into a maximum number of substrings based on the characters in an array.

Syntax:

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

Parameters:

  • separator: It is a character array that delimits the substrings in this string, an empty array that contains no delimiters, or null.
  • count: It is the maximum number of substring to return.
  • options: RemoveEmptyEntries option to omit empty array elements from the array returned or None option to include empty array elements in the array returned.

Return: It is an array whose elements contain the substrings in this string that are delimited by one or more characters in the separator.

Exceptions:

  • ArgumentOutOfRangeException: If the count is negative.
  • ArgumentException: If the options is not one of the StringSplitOptions values.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// Split(Char[], Int32, 
// StringSplitOptions) Method
using System;
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
  
        // Taking a string
        String str = "Geeks, For Geeks";
  
        char[] spearator = { ',', ' ' };
        Int32 count = 2;
  
        // Using the Method
        String[] strlist = str.Split(spearator, 
               count, StringSplitOptions.None);
  
        foreach(String s in strlist)
        {
            Console.WriteLine(s);
        }
    }
}

chevron_right


Output:

Geeks
 For Geeks

3. Split(String[], StringSplitOptions) Method

This method is used to splits a string into substrings based on the strings in an array. You can specify whether the substrings include empty array elements.

Syntax:

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

Parameters:

  • separator: It is a string array that delimits the substrings in this string, an empty array that contains no delimiters, or null.
  • options: RemoveEmptyEntries option to omit empty array elements from the array returned or None option to include empty array elements in the array returned.

Returns: This method returns an array of strings whose elements contain the substrings in this string that are delimited by one or more characters in the separator.

Exception: This method will give ArgumentException if the options parameter is not one of the StringSplitOptions values.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the 
// Split(String[], StringSplitOptions) 
// Method
using System;
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
  
        // Taking a string
        String str = "Geeks, For Geeks";
  
        String[] spearator = { "s,", "For" };
  
        // using the method
        String[] strlist = str.Split(spearator, 
           StringSplitOptions.RemoveEmptyEntries);
  
        foreach(String s in strlist)
        {
            Console.WriteLine(s);
        }
    }
}

chevron_right


Output:

Geek
 
 Geeks

4. Split(char[]) Method

This method is used to splits a string into substrings that are based on the characters in an array.

Syntax:

public String[] Split(char[] separator);

Here, separator is a character array that delimits the substrings in this string, an empty array that contains no delimiters, or null.

Returns: It returns an array of string whose elements contain the substrings in this string that are delimited by one or more characters in the separator.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the 
// Split(char[]) Method
using System;
  
class GFG {
      
    // Main Method
    static void Main(string[] args)
    {
          
        // Taking a string
        String str = "Geeks, For Geeks";
          
        char[] spearator = { ',', ' ' };
          
        // using the method
        String[] strlist = str.Split(spearator);
          
        foreach(String s in strlist)
        {
            Console.WriteLine(s);
        }
        Console.ReadKey();
    }
}

chevron_right


Output:

Geeks

For
Geeks

5. Split(char[], StringSplitOptions) Method

This method is used to splits a string into substrings based on the characters in an array. You can specify whether the substrings include empty array elements.

Syntax:

public String[] Split(char[] separator, StringSplitOptions option);

Parameters:

  • separator: It is a character array that delimits the substrings in this string, an empty array that contains no delimiters, or null.
  • options: RemoveEmptyEntries option to omit empty array elements from the array returned or None option to include empty array elements in the array returned.

Returns: This method returns an array whose elements contain the substrings in this string that are delimited by one or more characters in the separator .

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the use of
// Split(Char[], StringSplitOptions) method
using System;
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
  
        // Taking a string
        String str = "Geeks, For Geeks";
  
        char[] spearator = { ',', ' ' };
  
        // using the method
        String[] strlist = str.Split(spearator, 
           StringSplitOptions.RemoveEmptyEntries);
  
        foreach(String s in strlist)
        {
            Console.WriteLine(s);
        }
    }
}

chevron_right


Output:

Geeks
For
Geeks

6. Split(char[], Int32) Method

This method is used to 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.

Syntax:

public String[] Split(char[] separator, Int32 count);

Parameters:

  • separator: A character array that delimits the substrings in this string, an empty array that contains no delimiters, or null.
  • count: It is the maximum number of substring to return.

Returns: This method returns an array whose elements contain the substrings in this instance that are delimited by one or more characters in the separator.

Exception: This method will give ArgumentOutOfRangeException if the count is negative.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the use of
// Split(char[], Int32) Method
using System;
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
  
        // Taking a string
        String str = "Geeks, For Geeks";
  
        char[] spearator = { ',', ' ' };
        Int32 count = 2;
  
        // using the method
        String[] strlist = str.Split(spearator, count);
  
        foreach(String s in strlist)
        {
            Console.WriteLine(s);
        }
          
    }
}

chevron_right


Output:

Geeks
 For Geeks

Reference:



My Personal Notes arrow_drop_up

Competitive Programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.