using System.Collections;
//输入:要分隔的字符串,分隔符
//输出:字符串数组
public static string[] split(string strinput, string sp)
{
string tmp="";
int strlen=0, splen=0;
int found=0;
string[] rt = null;//返回数组
try
{
//输入判断
if(strinput==null || sp==null || strinput.Length==0 || sp.Length==0) return null;
//初始化一个数组列表(当做动态数组)
ArrayList tmp3 = new ArrayList();
strlen = strinput.Length;//获得输入字符串的长度
splen = sp.Length;//分隔符的长度
for(int i=0; i<strlen; i++)
{
//查找分隔符
found = strinput.IndexOf(sp, i);
if(found>=0)
{
tmp = "";
//取分隔符前的字符串
tmp = strinput.Substring(i, found-i);
//添加到数组列表
tmp3.Add(tmp);
i = found+splen-1;
}
else
{
string tmp2="";
//取最后的字符串
tmp2 = strinput.Substring(i);
if(tmp2 != "")
tmp3.Add(tmp2);
break;
}
}
//将动态数组的维数设置成实际存在的元素个数,因为数组列表是以16的倍数递增维数的
tmp3.TrimToSize();
//转换数组列表为字符串数组,并返回。
rt = (string[])tmp3.ToArray(typeof(String));
tmp3.Clear();
}
catch (Exception e)
{
//Console.WriteLine("{0}", e.Message);
throw e;
}
return rt;
}
http://www.gwuxiu.com