C#字符串分割

C#中字符串分割
有时我们需将一个字符串用另一个字符串来分割成字符串字组。而C#中string.split只提供用char来分割。怎么办?
用的时候直接调用第一个函数。

 

Code
/// <summary>
/// 将字符串分割成数组
/// </summary>
/// <param name="strSource"></param>
/// <param name="strSplit"></param>
/// <returns></returns>
public string[] StringSplit(string strSource,string strSplit)
{
string[] strtmp = new string[1];
int index = strSource.IndexOf(strSplit,0);
if(index<0)
{
strtmp[
0] = strSource;
return strtmp;
}
else
{
strtmp[
0] = strSource.Substring(0,index);
return StringSplit(strSource.Substring(index+strSplit.Length),strSplit,strtmp);
}
}
Code
/// <summary>
/// 采用递归将字符串分割成数组
/// </summary>
/// <param name="strSource"></param>
/// <param name="strSplit"></param>
/// <param name="attachArray"></param>
/// <returns></returns>
private string[] StringSplit(string strSource,string strSplit,string[] attachArray)
{
string[] strtmp = new string[attachArray.Length+1];
attachArray.CopyTo(strtmp,
0);

int index = strSource.IndexOf(strSplit,0);
if(index<0)
{
strtmp[attachArray.Length]
= strSource;
return strtmp;
}
else
{
strtmp[attachArray.Length]
= strSource.Substring(0,index);
return StringSplit(strSource.Substring(index+strSplit.Length),strSplit,strtmp);
}
}

 


你可能感兴趣的:(字符串)