asp.net Split分割字符串的方法


例如下面我要根据[phpstudy.net]分割的话

复制代码 代码如下:

string str = "reterry[phpstudy.net]是phpStudy[phpstudy.net]的站长";
string[] arrstr = str.Split(new char[] { '[', 's', 'o', 's', 'u', 'o', '8', '.', 'c', 'o', 'm', ']' });
for (int i = 0; i < arrstr.Length; i++)
{
Response.Write(arrstr[i]);
}

采用上面这种方法是很令人郁闷的,虽然得到的结果没错,但是其数组长度却是25,而不是3。下面这种方法是先将“[phpstudy.net]”替换成一个特殊字符,比如$,在根据这个字符执行Split
复制代码 代码如下:

string[] arrstr2 = str.Replace("[phpstudy.net]", "$").Split('$');


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3