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:

  1. Split the string into an array of strings based on the specified delimiter.
  2. You can specify multiple delimiters, or use an array of strings as delimiters.
  3. You can specify the maximum number of splits to limit the number of splits.
  4. You can specify to ignore white space characters when splitting.
  5. 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"}
Leave a Reply 0

Your email address will not be published. Required fields are marked *