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.
Given below are the examples of C# String Split():
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.
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.
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.
The above is the detailed content of C# String Split(). For more information, please follow other related articles on the PHP Chinese website!