All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
php+mysq 修改用户密码(用password加密)_[PHP教程]
PHP动态网页编程常用技巧四则_php资料_编程技术
ASP.NET2.0缓存(Cache)技术_[Asp.Net教程]
c#,MD5加密算法的一个实例_[Asp.Net教程]
《Effective,C#》之用委托实现回调_.net资料_编程技术
asp.net2.0内置Response对象的方法
ASP.NET实现进度条_[Asp.Net教程]
在.NET,2.0框架下动态创建Access数据库和表_[Asp.Net教程]
如何使用PHP和PEAR进行不同时区的转换_php资料_编程技术
WEB开发者需要了解的IE7的新功能_[Html教程]
Reading number is top 10 pictures
Female model behind the bitterness, often being overcharged5
A man's favorite things6
传销的好处
Seductive beauty of crime2
Ashlynn Brooke show proud chest measurement3
Group of female porn in 《westwards》, uninhibited woman threatened to not the bottom line1
全球清廉国家排行
The money of more than 100 countries and regions9
可爱的小动物
不知名的美女素颜照1
Download software ranking
Boxer vs Yellow4
Eclipse 4.2.1 For Win32
Ashlynn Video4
SQL2000 For 4IN1
c#程序设计案例教程
The king of fighters 97(Mobile phone games-apk)
Tram sex maniac 2 (H) rar bag15
尖东毒玫瑰B
小黑猫大战两米大花蛇
Sora aoi, the maid, students' uniforms
aaa published in(发表于) 2013/12/18 8:09:59 Edit(编辑)
在.NET中字符串替换的五种方法_.net资料_编程技术

在.NET中字符串替换的五种方法_.net资料_编程技术

在.NET中字符串替换的五种方法_.net资料_编程技术-你的首页-uuhomepage.com
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 !!!Replace = 0.178327413120941s
// ReplaceEx > vbReplace > vb > regexp

2¡¢string segment = "abcaBcabC";
regexp = 5.30117431126023s
vb = 2.46258449048692s
vbReplace = 2.5018721653171s
ReplaceEx = 1.00662179131705s !!!
Replace = 0.233760994763301s
// ReplaceEx > vb > vbReplace > regexp

3¡¢string segment = "abcaBcabCAbc";
regexp = 7.00987862982586s
vb = 3.61050301085753s
vbReplace = 3.42324876485699s
ReplaceEx = 1.14969947297771s !!!
Replace = 0.277254511397398s
// ReplaceEx > vbReplace > vb > regexp

4¡¢string segment = "ABCabcAbCaBcAbcabCABCAbcaBC";
regexp = 13.5940090151123s
vb = 11.6806222578568s
vbReplace = 11.1757614445411s
ReplaceEx = 1.70264153684337s !!!(my god!)
Replace = 0.42236820601501s
// ReplaceEx > vbReplace > vb > regexp

查看程序的Block在:





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.