手机
当前位置:查字典教程网 >编程开发 >C#教程 >C# String Replace高效的实例方法
C# String Replace高效的实例方法
摘要:复制代码代码如下:[ThreadStatic]staticchar[]mTempChars;protectedstaticchar[]Get...

复制代码 代码如下:

[ThreadStatic]

static char[] mTempChars;

protected static char[] GetTempData()

{

if (mTempChars == null)

mTempChars = new char[1024 * 64];

return mTempChars;

}

public static string Replace(string value, string oldData, string newData)

{

char[] tmpchars = GetTempData();

int newpostion = 0;

int oldpostion = 0;

int length = value.Length;

int oldlength = oldData.Length;

int newlength = newData.Length;

int index = 0;

int copylength = 0;

bool eq = false;

while (index < value.Length)

{

eq = true;

for (int k = 0; k < oldlength; k++)

{

if (value[index + k] != oldData[k])

{

eq = false;

break;

}

}

if (eq)

{

copylength = index - oldpostion;

value.CopyTo(oldpostion, tmpchars, newpostion, copylength);

newpostion += copylength;

index += oldlength;

oldpostion = index;

newData.CopyTo(0, tmpchars, newpostion, newlength);

newpostion += newlength;

}

else

{

index++;

}

}

if (oldpostion < length)

{

copylength = index - oldpostion;

value.CopyTo(oldpostion, tmpchars, newpostion, copylength);

newpostion += copylength;

}

return new string(tmpchars, 0, newpostion);

}

【C# String Replace高效的实例方法】相关文章:

C#委托初级使用的实例代码

c#生成缩略图的实现方法

c# StringBuilder.Replace 方法 (Char, Char, Int32, Int32)

C#图片压缩的实现方法

c#简单读取文本的实例方法

C# 静态变量与静态方法实例研究

C# dynamic关键字的使用方法

C# 将字节流转换为图片的实例方法

C#生成sitemap站点地图的方法

C# WinForm中Panel实现用鼠标操作滚动条的实例方法

精品推荐
分类导航