To split a string into an array of words in C#, you can use the Split()
method provided by the String
class. This method splits the string into an array of substrings based on a specified delimiter character or a set of delimiter characters.
In the following example, we split a string into an array of words using space as the delimiter character:
main.cs134 chars3 lines
The resulting words
array contains the following elements:
main.cs72 chars2 lines
You can also split a string using multiple delimiter characters, such as space and comma, by passing an array of delimiter characters to the Split()
method:
main.cs145 chars4 lines
The resulting words
array contains the following elements:
main.cs76 chars2 lines
Note that the second element in the array is an empty string because there is a comma immediately after the word "fox".
gistlibby LogSnag