首页 新闻 赞助 找找看

字符串快速替换 解决方案

0
[已解决问题] 解决于 2010-07-14 17:07

如题: string test="abcdefgcd";

现在要将第一个c 替换成mmm  (当然这里肯定是指定具体位置)

如何才能快速替换,谁帮忙写个封装的方法

like%'远远'%的主页 like%'远远'% | 小虾三级 | 园豆:635
提问于:2010-07-14 16:23
< >
分享
最佳答案
0

static string ReplaceString(string oldstr, string filterstring,string newstring)
{
string result = string.Empty;
if (string.IsNullOrEmpty(oldstr) || string.IsNullOrEmpty(filterstring) ||newstring==null)
{
return result;
}
int position = oldstr.IndexOf(filterstring);
if (position <= 0) { return result; }
//first position
string beginstr = oldstr.Substring(0, position + filterstring.Length);
string endstr=oldstr.Substring(position+filterstring.Length);
beginstr
= beginstr.Replace(filterstring, newstring);
result
= beginstr + endstr;
return result;
}

 

string test="abccdefgccd";
Console.WriteLine(ReplaceString(test,
"cc", "mmm"));

 

结果:
abmmmdefgccd

邀月 | 高人七级 |园豆:25475 | 2010-07-14 16:52
位置是否需求另指定。
Astar | 园豆:40805 (高人七级) | 2010-07-14 17:20
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册