在C#中,split方法是用于將字符串按照指定的分隔符拆分成多個子字符串的功能。一般用法如下:
示例代碼如下:
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"}