Updated March 29, 2023
Introduction to C# String Split()
The method used to divide a given string which is separated by the delimiters for split into an array of strings, is called the C# String Split() method, with the delimiters of the split being an array consisting of strings or an array consisting of characters or just characters and an array of strings consisting of the substrings whose delimiters are an array of Unicode characters or elements of the specified string, is returned by using the Split() method in C# and ArgumentOutofRangeException and ArgumentException exceptions are raised as part of exception handling when using this method.
Syntax
The syntax of the C# String Split() method is as follows:
public String[] Split(String[] separator, int count, StringSplitOptions options);
public String[] Split(params Char[] character)
public String[] Split(Char[], Int32)
public String[] Split(Char[], Int32, StringSplitOptions)
public String[] Split(Char[], StringSplitOptions)
public String[] Split(String[], Int32, StringSplitOptions)
public String[] Split(String[], StringSplitOptions)
Where separator is the array of strings delimiting the substrings in the given string
The count keeps count of the maximum number of substrings to be returned
Options can remove the empty entries option to discard the array elements that are empty from the returned array or the options none to include the empty array elements from the returned array.
Working of C# String Split() method
- Whenever there is a need to divide the string based on the delimiter separating the array of strings or array of characters or just characters, we make use of the String split() method.
- The delimiters separating the strings can be an array of strings or an array of characters, or just characters.
- The substring of the given string separated based on the given delimiter is returned by using a string split() method.
- ArgumentOutofRangeException and ArgumentException exceptions are raised as part of exception handling when using the Split() method.
Examples of C# String Split()
Given below are the examples of C# String Split():
Example #1
C# program to demonstrate String Split() method to separate the string separated by a comma
Code:
using System;
//a class called check is defined
public class check
{
//main method is called
public static void Main(string[] args)
{
//a string variable is used to store the string consisting of delimiters
string str = "Welcome,to,C,Sharp";
//a string array is used to store the array of strings returned by using string split method
string[] str2 = str.Split(',');
foreach (string s in str2)
{
Console.WriteLine(s);
}
}
}
Output:
In the above program, a class called check is called. Then the main method is called, within which a string variable is defined to store the string consisting of delimiters which can be used to separate the given string into an array of substrings, and then a string array is defined to store the array of substrings returned by using string split() method which is displayed as output. The output is shown in the snapshot above.
Example #2
C# program to demonstrate string split() method to separate the given string consisting of multiple delimiters into an array of strings:
Code:
using System;
//a namespace called program is defined
namespace program
{
//a class called check is defined
class check
{
//main method is called
static void Main(string[] args)
{
//a string variable is used to store the string consisting of multiple delimiters
string str1 = "Welcome,to-C|Sharp";
//a string array is defined to store the array of substrings returned by using the split() method on the given string
string[] str2 = str1.Split(new char[] { ',', '-', '|' }, StringSplitOptions.RemoveEmptyEntries);
for (int j = 0; j < str2.Length; j++)
{
Console.WriteLine(str2[j]);
}
Console.WriteLine("\nThis is how split() method works");
Console.ReadLine();
}
}
}
Output:
In the above program, a namespace called program is created. Then the class called check is defined within which the main method is called. Then a string variable is defined to store the string consisting of multiple delimiters. Then we use the split() method to separate the given string into an array of substrings displayed as the output. The output is shown in the snapshot above.
Example #3
C# program to demonstrate String IndexOf() method to separate the given string consisting of multiple delimiters and store the return value in a list:
Code:
using System;
using System.Collections.Generic;
//a namespace called program is created
namespace program
{
//a class called check is defined
class check
{
//main method is called
static void Main(string[] args)
{
//a string variable is defined to store the string consisting of multiple delimiters
string str = "Welcome-to,C|Sharp";
//a list is defined to store the substrings separated from the given string consisting of delimiters
IList<string> listname = new List<string>(str.Split(new char[] { '-', ',', '|' }, StringSplitOptions.RemoveEmptyEntries));
for (int j = 0; j < listname.Count; j++)
{
Console.WriteLine(list[j]);
}
Console.WriteLine("\nThis is how split method works");
Console.ReadLine();
}
}
}
Output:
In the above program, a namespace called program is created. Then a class called check is defined within which the main method is called. Then a string variable is defined to store the string consisting of multiple delimiters. Then, by using a string split() method, the given string can be split into an array of strings stored in a list by creating a new list. The output is shown in the snapshot above.
Recommended Articles
This is a guide to C# String Split(). Here we discuss the concept of the String Split method in C# through the definition of string split method, the syntax of string split method, and working of String split method through programming examples and their outputs. You may also have a look at the following articles to learn more –