What functions does split have in C#?
In C#, the split method is used to separate a string into multiple substrings based on a specified delimiter. Typically used as follows:
- Split the string into an array of strings based on the specified delimiter.
- You can specify multiple delimiters, or use an array of strings as delimiters.
- You can specify the maximum number of splits to limit the number of splits.
- You can specify to ignore white space characters when splitting.
- You can use the StringSplitOptions enumeration to control whether empty strings are retained during the split.
The code example is provided below:
string sentence = "Hello,world";
string[] words = sentence.Split(',');
// words = {"Hello", "world"}
string sentence2 = "apple,banana;orange";
string[] words2 = sentence2.Split(new char[] { ',', ';' });
// words2 = {"apple", "banana", "orange"}
string sentence3 = "one two three four five";
string[] words3 = sentence3.Split(new char[] { ' ' }, 3);
// words3 = {"one", "two", "three four five"}
string sentence4 = "apple, ,banana, ,orange";
string[] words4 = sentence4.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
// words4 = {"apple", "banana", "orange"}