C# String Replace高效的實(shí)例方法
更新時間:2013年05月01日 13:56:52 作者:
C# String Replace高效的實(shí)例方法,需要的朋友可以參考一下
復(fù)制代碼 代碼如下:
[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);
}
您可能感興趣的文章:
- javascript模擬實(shí)現(xiàn)C# String.format函數(shù)功能代碼
- C#實(shí)現(xiàn)去除Strings中空格的方法
- C#中string和StingBuilder內(nèi)存中的區(qū)別實(shí)例分析
- C#中String與string的區(qū)別分析
- C#自定義函數(shù)NetxtString生成隨機(jī)字符串
- C#中把字符串String轉(zhuǎn)換為整型Int的小例子
- C#中StringBuilder類的使用總結(jié)
- 淺析C#中的Main(String[] args)參數(shù)輸入問題
- C#中string.format用法詳解
相關(guān)文章
C#操作DataTable方法實(shí)現(xiàn)過濾、取前N條數(shù)據(jù)及獲取指定列數(shù)據(jù)列表的方法
這篇文章主要介紹了C#操作DataTable方法實(shí)現(xiàn)過濾、取前N條數(shù)據(jù)及獲取指定列數(shù)據(jù)列表的方法,實(shí)例分析了C#操作DataTable的各種常用技巧,非常具有實(shí)用價值,需要的朋友可以參考下2015-04-04C#使用async和await實(shí)現(xiàn)異步編程
本文詳細(xì)講解了C#使用async和await實(shí)現(xiàn)異步編程的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07