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"));
结果: