当前位置: 代码迷 >> 综合 >> C#如何替换字符串中第一个出现的指定字符串?
  详细解决方案

C#如何替换字符串中第一个出现的指定字符串?

热度:57   发布时间:2024-02-09 18:24:17.0

Regex r = new Regex(childstr);
str = r.Replace(str, "", 1);

 

已知一个字符串,比如asderwsde,寻找其中的一个子字符串比如sde 的个数,如果没有返回0,有的话返回子字符串的个数

public static int getnum(string str,string childstr){int num = 0;if (str == "" || childstr == ""){return num;}while (str.IndexOf(childstr) > 0){Regex r = new Regex(childstr);str = r.Replace(str, "", 1);num++;}return num;}

 

  相关解决方案