专著于富媒体技术~
本站某些作品来源于互联网,如果侵犯了您的利益,请留言说明!
本站某些作品来源于互联网,如果侵犯了您的利益,请留言说明!
[此文来源于互联网,牛C网只负责收集整理]
因为C#的SPLIT局限性比较大,只能用字符或字符数组来做分隔符。没有提供像VB一样的可以用字符串做分割符的SPLIT函数,这样在写程序过程中,少了很多便利。 于是,就写了如下一个函数来方便自己编程。
程序如下:
=====================================================
using System;
using System.Collections;
namespace split
...{
class mySplit
...{
[STAThread]
static void Main(string[] args)
...{
string str1="",str2="";
if(args.Length != 2)
...{
Console.WriteLine("请输入要分割的字符串:");
str1 = Console.ReadLine();
Console.WriteLine("请输入分割符:");
str2 = Console.ReadLine();
Console.WriteLine(" 分割出的数据如下: ");
}
else
...{
str1 = args[0];
str2 = args[1];
}
string[] output = null;
output = split(str1,str2);
for (int i=0; i < output.Length; i )
...{
Console.WriteLine("{0}", output[i]);
}
Console.WriteLine(" 被分割成{0}份字符串!", output.Length);
TimeSpan sp1,sp2,sp3;
DateTime dt = DateTime.Now;
for(int i=0; i<1000; i )
...{
output=split(str1,str2);
output=null;
}
sp1=DateTime.Now - dt;
//用&&符分割!
System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex("&{2}");
dt = DateTime.Now;
for(int j=0; j<1000; j )
...{
output=rg.Split(str1);
output=null;
}
sp2=DateTime.Now - dt;
Console.WriteLine(" 正则式类执行时间:{0}", sp2.ToString());
Console.WriteLine("自制函数执行时间:{0}", sp1.ToString());
Console.WriteLine(" 输入回车退出程序!");
Console.Read();
}
//分割函数开始
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
...{
//查找分隔符
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;
}
}
}
using System;
using System.Collections;
namespace split
...{
class mySplit
...{
[STAThread]
static void Main(string[] args)
...{
string str1="",str2="";
if(args.Length != 2)
...{
Console.WriteLine("请输入要分割的字符串:");
str1 = Console.ReadLine();
Console.WriteLine("请输入分割符:");
str2 = Console.ReadLine();
Console.WriteLine(" 分割出的数据如下: ");
}
else
...{
str1 = args[0];
str2 = args[1];
}
string[] output = null;
output = split(str1,str2);
for (int i=0; i < output.Length; i )
...{
Console.WriteLine("{0}", output[i]);
}
Console.WriteLine(" 被分割成{0}份字符串!", output.Length);
TimeSpan sp1,sp2,sp3;
DateTime dt = DateTime.Now;
for(int i=0; i<1000; i )
...{
output=split(str1,str2);
output=null;
}
sp1=DateTime.Now - dt;
//用&&符分割!
System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex("&{2}");
dt = DateTime.Now;
for(int j=0; j<1000; j )
...{
output=rg.Split(str1);
output=null;
}
sp2=DateTime.Now - dt;
Console.WriteLine(" 正则式类执行时间:{0}", sp2.ToString());
Console.WriteLine("自制函数执行时间:{0}", sp1.ToString());
Console.WriteLine(" 输入回车退出程序!");
Console.Read();
}
//分割函数开始
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
...{
//查找分隔符
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;
}
}
}
经测试,本函数比用正则式的SPLIT快一倍!
作者:gdgzboy@牛C网
地址:http://www.niuc.net/post/1360/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
牛C网推荐您再看看以下日志:
两种方法访问多层嵌套的frame
让Asp.NET的DataGrid可排序、可选择、可分页
部署水晶报表的打包安装
ASP.NET实现匿名访问控制
ADO.NET快速起步
ASP.NET实现下拉框二级联动组件
使用ASP.NET制作功能完善的安装程序
ASP.NET中TreeView控件使用小结
ASP.NET入门数据篇
VS2005 SQL2005 ASP.NET2.0数据库连接
两种方法访问多层嵌套的frame
让Asp.NET的DataGrid可排序、可选择、可分页
部署水晶报表的打包安装
ASP.NET实现匿名访问控制
ADO.NET快速起步
ASP.NET实现下拉框二级联动组件
使用ASP.NET制作功能完善的安装程序
ASP.NET中TreeView控件使用小结
ASP.NET入门数据篇
VS2005 SQL2005 ASP.NET2.0数据库连接
上一篇



