为了账号安全,请及时绑定邮箱和手机立即绑定

字符串快速替换 解决方案

字符串快速替换 解决方案

波斯汪 2018-10-10 05:06:42
如题: string test="abcdefgcd";现在要将第一个c 替换成mmm  (当然这里肯定是指定具体位置)如何才能快速替换,谁帮忙写个封装的方法
查看完整描述

1 回答

?
慕尼黑5688855

TA贡献1848条经验 获得超2个赞

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


查看完整回答
反对 回复 2018-10-22
  • 1 回答
  • 0 关注
  • 400 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号