Linux 拨号vps windows公众号手机端

c#中split的功能有哪些

lewis 9年前 (2016-11-20) 阅读数 8 #程序编程
文章标签 c#

在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"}
版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门