快速替换字符串 c#
来源:互联网 发布:资金控盘指标源码 编辑:程序博客网 时间:2024/05/22 06:54
在.NET中字符串替换有以下五种方法:
1:使用String.Replace函数替换,但不支持大小写。
2:正则System.Text.Regex 替换,用RegExpOption修改是否支持大小写。
3:在小数据的情况下,使用String.SubString 和+可以实现间接替换。
4:导入Microsoft Visual Basic RunTime (Microsoft.VisualBasic.DLL) 使用Strings.Replace速度很快。
5:参照反射Reflector.FileDisassembler配合Strings.Split and Strings.Join 等实现,速度同5。
一下介绍一种算法,类似KMP算法。有兴趣的参照研究下。
private static string ReplaceEx(string original,
string pattern, string replacement)
{
int count, position0, position1;
count = position0 = position1 = 0;
string upperString = original.ToUpper();
string upperPattern = pattern.ToUpper();
int inc = (original.Length/pattern.Length) *
(replacement.Length-pattern.Length);
char [] chars = new char[original.Length + Math.Max(0, inc)];
while( (position1 = upperString.IndexOf(upperPattern,
position0)) != -1 )
{
for ( int i=position0 ; i < position1 ; ++i )
chars[count++] = original[i];
for ( int i=0 ; i < replacement.Length ; ++i )
chars[count++] = replacement[i];
position0 = position1+pattern.Length;
}
if ( position0 == 0 ) return original;
for ( int i=position0 ; i < original.Length ; ++i )
chars[count++] = original[i];
return new string(chars, 0, count);
}
测试
static void Main(string[] args)
{
string segment = "AaBbCc";
string source;
string pattern = "AbC";
string destination = "Some";
string result = "";
const long count = 1000;
StringBuilder pressure = new StringBuilder();
HiPerfTimer time;
for (int i = 0; i < count; i++)
{
pressure.Append(segment);
}
source = pressure.ToString();
GC.Collect();
//regexp
time = new HiPerfTimer();
time.Start();
for (int i = 0; i < count; i++)
{
result = Regex.Replace(source, pattern,
destination, RegexOptions.IgnoreCase);
}
time.Stop();
Console.WriteLine("regexp = " + time.Duration + "s");
GC.Collect();
//vb
time = new HiPerfTimer();
time.Start();
for (int i = 0; i < count; i++)
{
result = Strings.Replace(source, pattern,
destination, 1, -1, CompareMethod.Text);
}
time.Stop();
Console.WriteLine("vb = " + time.Duration + "s");
GC.Collect();
//vbReplace
time = new HiPerfTimer();
time.Start();
for (int i = 0; i < count; i++)
{
result = VBString.Replace(source, pattern,
destination, 1, -1, StringCompareMethod.Text);
}
time.Stop();
Console.WriteLine("vbReplace = " + time.Duration + "s");// + result);
GC.Collect();
// ReplaceEx
time = new HiPerfTimer();
time.Start();
for (int i = 0; i < count; i++)
{
result = Test.ReplaceEx(source, pattern, destination);
}
time.Stop();
Console.WriteLine("ReplaceEx = " + time.Duration + "s");
GC.Collect();
// Replace
time = new HiPerfTimer();
time.Start();
for (int i = 0; i < count; i++)
{
result = source.Replace(pattern.ToLower(), destination);
}
time.Stop();
Console.WriteLine("Replace = " + time.Duration + "s");
GC.Collect();
//sorry, two slow :(
/*//substring
time = new HiPerfTimer();
time.Start();
for (int i = 0; i < count; i++)
{
result = StringHelper.ReplaceText(source, pattern,
destination, StringHelper.CompareMethods.Text);
}
time.Stop();
Console.WriteLine("substring =" + time.Duration + ":");
GC.Collect();
//substring with stringbuilder
time = new HiPerfTimer();
time.Start();
for (int i = 0; i < count; i++)
{
result = StringHelper.ReplaceTextB(source, pattern,
destination, StringHelper.CompareMethods.Text);
}
time.Stop();
Console.WriteLine("substringB=" + time.Duration + ":");
GC.Collect();
*/
Console.ReadLine();
}
1¡¢string segment = "abcaBc";
regexp = 3.75481827997692s
vb = 1.52745502570857s
vbReplace = 1.46234256029747s
ReplaceEx = 0.797071415501132s !!!<FONT color=gray>Replace = 0.178327413120941s </FONT>
// ReplaceEx > vbReplace > vb > regexp
2¡¢string segment = "abcaBcabC";
regexp = 5.30117431126023s
vb = 2.46258449048692s
vbReplace = 2.5018721653171s
ReplaceEx = 1.00662179131705s !!!
<FONT color=gray>Replace = 0.233760994763301s </FONT>
// ReplaceEx > vb > vbReplace > regexp
3¡¢string segment = "abcaBcabCAbc";
regexp = 7.00987862982586s
vb = 3.61050301085753s
vbReplace = 3.42324876485699s
ReplaceEx = 1.14969947297771s !!!
<FONT color=gray>Replace = 0.277254511397398s </FONT>
// ReplaceEx > vbReplace > vb > regexp
4¡¢string segment = "ABCabcAbCaBcAbcabCABCAbcaBC";
regexp = 13.5940090151123s
vb = 11.6806222578568s
vbReplace = 11.1757614445411s
ReplaceEx = 1.70264153684337s !!!(my god!)
<FONT color=gray>Replace = 0.42236820601501s</FONT>
// ReplaceEx > vbReplace > vb > regexp
查看程序的Block在:
string upperPattern = pattern.ToUpper();
如果需要敏感,就免了这2行。
解释:先建一个char[]类型的变量采访替换后的字符,其大小就是最大可能被替换的字符,例如ABABAB,替换AB成C,其获取过程就是ABABAB最大可能包括的AB的数目乘以AB多于C的数目,
,inc不一定大于零。
然后循环,用IndexOf索引。赋值。。。判断,返回。
- 快速替换字符串 c#
- C# 替换字符串
- c#字符串替换函数
- C#替换字符串中子串
- c# 字符串替换 忽略大小写
- C#替换字指定字符串
- 快速修改替换字符串工具ultraEdit
- Linux下快速替换文本中的字符串
- c# Datagrid 中字段替换字符串
- C#在word文档中替换字符串
- C#在word文档中替换字符串
- C#字符串替换_无视大小写
- C#正则表达式匹配替换字符串
- C# 字符串替换方法/(正则表达式)
- C#字符串处理:截取、替换、移除
- C# richtextbox查找和替换指定字符串
- C#字符串处理:裁剪,替换,移除
- C#有关字符串的分割,替换,截取
- BCB中TrayIcon组件的使用
- ExtJs gridpanel 中的按钮列(2)
- RETON ADS应用发布系统在高校的应用
- PHP内存设置
- 增加字典类型
- 快速替换字符串 c#
- Oracle数据库连接技术参考
- 使用VSTS进行单元测试
- sqlserver 获取数据字典的经典脚本
- 取消wfp,替换系统文件
- 使用SOCKET API写的客户端与服务端测试程序(C语言)
- MTK自带的XML解析
- C#中怎么复制数组?C#复制数组的5种方法!
- 使用word时,想在中间补充一个字符,可是却自动删除了后面一个字符