字符串截取一种是字符串string自带方法Split:
string x = "abc,efg;cd,fg";
string[] y = x.Split(new char[]{ ',' }, StringSplitOptions.RemoveEmptyEntries);//去除掉空的字符串string[] z = x.Split(new char[]{ ',' , ';' }, StringSplitOptions.RemoveEmptyEntries);输出结果:y = { "abc" , "efg;cd" , "fg" }
z = { "abc" , "efg" , "cd" , "fg" }
但是结果有时不是我所想要的,比如:
string x = "abcde != x";
要想用"!="字符串来分割字符串,即另一种截取方法,可以采用正则表达式:
using System.Text.RegularExpressions;
string x = "abcde != x";
string[] t = Regex.Split(x, "!=", RegexOptions.IgnoreCase);
输出结果:t = { "abcde " , " x" }
总结:
x.Split(new char[]{ ',' , ';' }, StringSplitOptions.RemoveEmptyEntries);
->分别按照多个字符,分割多次得到结果
using System.Text.RegularExpressions;
Regex.Split(x, "!=", RegexOptions.IgnoreCase);
->按照字符串分割一次源字符串