信息交流、传播、提炼

nice to meet you

博客园 首页 新随笔 联系 订阅 管理

C#中常用Split对字符串进行分割,其函数原型:

[ComVisibleAttribute(false)]
public string[] Split (
    string[] separator,
    StringSplitOptions options
)

separator :分隔此字符串中子字符串的字符串数组。

options :指定 RemoveEmptyEntries 以省略返回的数组中的空数组元素,或指定 None 以包含返回的数组中的空数组元素。

常用的方式如下:

(1)字符数组分割

string s = "ABcdEF";

string t1[] = s.Split('c');

string t2[] = s.Split(new char[]{'c','d'});

(2)正则表达式分割

string s = "ABcdEF";

Regex r = new Regex("cd");

string t1[] = r.Split(s);

分割结果可能会产生部分空格数组,通过s.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries)去掉。

posted on 2008-11-28 21:19  seeyou  阅读(381)  评论(0)    收藏  举报