In c#, the string Split method is used to split a string into substrings based on the array's characters. The split method will return a string array that contains a substring that is delimited by the specified characters in an array.
Following is the pictorial representation of split method functionality in the c# programming language.
If you observe the above diagram, we split the string “Suresh-Rohini-Trishika” with the delimiter “-” using the Split method. Once splitting is done, then the split method will return a string array as shown above.
Following is the syntax of defining a split method in the c# programming language.
If you observe syntax, we use a character array (char[]) to define delimiters to split the given string into substrings and return it as a string array.
Following is the example of splitting the given string with a comma (“,”) delimiter using the Split() method in the c# programming language.
If you observe the above example, we used a split() method to split the given string ("Suresh, Rohini, Trishika") with comma (',') delimiter and returning substrings as a string array. Here we used a for loop to iterate through string array to display array elements.
When you execute the above c# program, you will get the result as shown below.
This is how you can split the string with required delimiters using the split() method in the c# programming language.
In the above example, we used only one comma delimiter with a split() method to split the given string. You can even use multiple delimiters with the split() method to split the given string and remove empty subscripts in the string array.
Following is the example of splitting the given string with multiple delimiters in the c# programming language.
If you observe the above example, we used a split() method with multiple delimiters to split the string into a string array.
Here, the StringSplitOptions.RemoveEmptyEntries property is used to remove empty string elements while returning the result array.
When you execute the above c# program, you will get the result below.
This is how you can split the given string with multiple delimiters using the split() method in the c# programming language.
Generally, the split method in c# will return the result as a string array. If we want to return a result as a list, then we can convert a string array to a list using the List object.
Following is the example to return the split method result as a list in the c# programming language.
If you observe the above example, we convert a split method string array result as a list using the List object. We will learn more about List in the next chapters.
When you execute the above c# program, you will get the result below.
This is how you can return the split method result as a list based on your requirements in the c# programming language.