溫馨提示×

c#中split的功能有哪些

c#
小億
98
2024-03-06 09:29:06
欄目: 編程語言

在C#中,split方法是用于將字符串按照指定的分隔符拆分成多個子字符串的功能。一般用法如下:

  1. 將字符串按照指定的分隔符拆分成字符串數組。
  2. 可以指定多個分隔符,或者使用字符串數組作為分隔符。
  3. 可以指定最大拆分次數,以限制拆分的次數。
  4. 可以指定拆分時忽略空白字符。
  5. 可以使用 StringSplitOptions 枚舉來控制拆分時是否保留空字符串。

示例代碼如下:

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"}

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女