String.Split() Method in C# with Examples

23 Jul, 2019
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:
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:
// C# program to illustrate the // Split(String[], Int32, StringSplitOptions)// Methodusing 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);        }    }}
Output:
Geek
 Geeks

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:
// C# program to illustrate the// Split(Char[], Int32, // StringSplitOptions) Methodusing 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);        }    }}
Output:
Geeks
 For Geeks

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:
// C# program to illustrate the // Split(String[], StringSplitOptions) // Methodusing 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);        }    }}
Output:
Geek
 
 Geeks

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:
// C# program to illustrate the // Split(char[]) Methodusing 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();    }}
Output:
Geeks

For
Geeks

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:
// C# program to illustrate the use of// Split(Char[], StringSplitOptions) methodusing 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);        }    }}
Output:
Geeks
For
Geeks

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:
// C# program to illustrate the use of// Split(char[], Int32) Methodusing 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);        }              }}
Output:
Geeks
 For Geeks

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.string.split?view=netframework-4.8
CSharp-method
CSharp-string
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# | Multiple inheritance using interfaces
Introduction to .NET Framework
C# | Delegates
Differences Between .NET Core and .NET Framework
C# | Data Types
C# | Constructors
C# | Class and Object
C# | Replace() Method
Extension Method in C#
C# | Encapsulation