C# split函数用法迷惑
举个例子:stringmyString="HelloWorld!";string[]myWords=myString.Split(newchar[2]{'e','o'})...
举个例子:
string myString = "Hello World!";
string[] myWords = myString.Split(new char[2] { 'e','o'});
值为myword[0]="H";
myword[1]="ll";
myword[2]=" W";
myword[3]="rld!";
如果string myString = "Hello World!";
string[] myWords = myString.Split(new char[2] { 'e','o','l'});
按整理结果应该是myword[0]="H";
myword[1]=" W";
myword[2]="r";
myword[3]="d!";
但程序执行结果为
myword[0]="H";
myword[1]="";
myword[2]="";
myword[3]="";
myword[4]=" W";
myword[5]="r";
myword[6]="d!";
请问高手这是怎么来的?谢谢
哦对了,最下面一个数组长度写错了,长度应该是3
还是不明白 展开
string myString = "Hello World!";
string[] myWords = myString.Split(new char[2] { 'e','o'});
值为myword[0]="H";
myword[1]="ll";
myword[2]=" W";
myword[3]="rld!";
如果string myString = "Hello World!";
string[] myWords = myString.Split(new char[2] { 'e','o','l'});
按整理结果应该是myword[0]="H";
myword[1]=" W";
myword[2]="r";
myword[3]="d!";
但程序执行结果为
myword[0]="H";
myword[1]="";
myword[2]="";
myword[3]="";
myword[4]=" W";
myword[5]="r";
myword[6]="d!";
请问高手这是怎么来的?谢谢
哦对了,最下面一个数组长度写错了,长度应该是3
还是不明白 展开
展开全部
经过new char[2] { 'e','o'}分隔后
myword[0]="H";
myword[1]="ll";
myword[2]=" W";
myword[3]="rld!";
new char[3] { 'e','o','l'}是在new char[2] { 'e','o'}分隔基础上再次以'l'分隔,那么"ll"就被分隔成"","",""了,其它项不变,于是
myword[0]="H";
myword[1]="";
myword[2]="";
myword[3]="";
myword[4]=" W";
myword[5]="r";
myword[6]="d!";
myword[0]="H";
myword[1]="ll";
myword[2]=" W";
myword[3]="rld!";
new char[3] { 'e','o','l'}是在new char[2] { 'e','o'}分隔基础上再次以'l'分隔,那么"ll"就被分隔成"","",""了,其它项不变,于是
myword[0]="H";
myword[1]="";
myword[2]="";
myword[3]="";
myword[4]=" W";
myword[5]="r";
myword[6]="d!";
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询