C# String Replace高效的实例方法
更新时间:2013年05月01日 13:56:52 作者:
C# String Replace高效的实例方法,需要的朋友可以参考一下
复制代码 代码如下:
[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);
}
相关文章
解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
本篇文章是对从源码分析常见的基于Array的数据结构动态扩容机制进行了详细的分析介绍,需要的朋友参考下2013-05-05
C#中static void Main(string[] args) 参数示例详解
这篇文章主要介绍了C#中static void Main(string[] args) 参数详解,本文通过具体示例给大家介绍的非常详细,需要的朋友可以参考下2017-03-03


最新评论