亚洲3P视频,日韩BBW无码,亚洲制服麻豆网站,88re伊人,九草精品视频在线观看,国产精品久久夜,色青青狠狠色,无码熟女一区二区三区,日本一区二区成人网站

新聞建站cms系統(tǒng)、政府cms系統(tǒng)定制開(kāi)發(fā)

廣州網(wǎng)站建設(shè)公司-閱速公司

asp.net新聞發(fā)布系統(tǒng)、報(bào)紙數(shù)字報(bào)系統(tǒng)方案
/
http://www.duxiu2008.cn/
廣州網(wǎng)站建設(shè)公司
您當(dāng)前位置:首頁(yè)>ASP.NET MVC

ASP.NET MVC

.Net(C#) 實(shí)現(xiàn)replace字符串替換只替換一次的方法

發(fā)布時(shí)間:2021/9/26 17:30:20  作者:Admin  閱讀:974  

廣告:

本文主要介紹.Net(C#)替換字符串時(shí),實(shí)現(xiàn)replace替換字符串只替換一次的方法代碼。分別通過(guò)StringBuilder、正則表達(dá)式(Regex)、IndexOf和Substring實(shí)現(xiàn),并且可以通過(guò)擴(kuò)展方法簡(jiǎn)化代碼方便調(diào)用。

1、使用StringBuilder替換

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
  class Program
  {
    static void Main(string[] args)
    {
      string str = "hello world! cjavapy!!!";
      StringBuilder sb = new StringBuilder(str);
      str = sb.Replace("!", "b", 0, str.IndexOf("!") + 1).ToString(); ;//指定替換的范圍實(shí)現(xiàn)替換一次,并且指定范圍中要只有一個(gè)替換的字符串
      Console.WriteLine(str);//輸出:hello worldb cjavapy!!!
      Console.ReadKey();
    }
  }
}

2、使用正則表達(dá)式(Regex)替換

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
  class Program
  {
    static void Main(string[] args)
    {
      string str = "hello world! cjavapy!!!";
      Regex regex = new Regex("!");//要替換字符串"!"
      str = regex.Replace(str, "b", 1);//最后一個(gè)參數(shù)是替換的次數(shù)
      Console.WriteLine(str);//輸出:hello worldb cjavapy!!!
      Console.ReadKey();
    }
  }
}

3、使用IndexOf和Substring替換

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
  class Program
  {
    static void Main(string[] args)
    {
      string str = "hello world! cjavapy!!!";
      StringBuilder sb = new StringBuilder(str);
      int index = str.IndexOf("!");
      str = str.Substring(0, index) + "b" + str.Substring(index + 1);//指定替換的范圍實(shí)現(xiàn)替換一次,并且指定范圍中要只有一個(gè)替換的字符串
      Console.WriteLine(str);//輸出:hello worldb cjavapy!!!
      Console.ReadKey();
    }
  }
}

4、通過(guò)擴(kuò)展方法實(shí)現(xiàn)ReplaceOne

擴(kuò)展方法實(shí)現(xiàn)代碼

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication2
{
public static class StringReplace
{
public static string ReplaceOne(this string str, string oldStr, string newStr)
{
StringBuilder sb = new StringBuilder(str);
int index = str.IndexOf(oldStr);
if (index > -1)
return str.Substring(0, index) + newStr + str.Substring(index + oldStr.Length);
return str;
}
}
}

調(diào)用代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication2
{
  class Program
  {
    static void Main(string[] args)
    {
      string str = "hello world! cjavapy!!!";
      str = str.ReplaceOne("!","b");//通過(guò)擴(kuò)展方法替換
      Console.WriteLine(str);//輸出:hello worldb cjavapy!!!
      Console.ReadKey();
    }
  }
}

廣告:

相關(guān)文章
replace
cms新聞系統(tǒng)購(gòu)買咨詢
掃描關(guān)注 廣州閱速軟件科技有限公司
掃描關(guān)注 廣州閱速科技
兖州市| 昌乐县| 佛坪县| 铜鼓县| 资中县| 武胜县| 仙居县| 广东省| 昭苏县| 湘阴县| 阿拉尔市| 堆龙德庆县| 崇明县| 峨眉山市| 平邑县| 淮阳县| 惠来县| 沽源县| 游戏| 漠河县| 伊宁市| 奇台县| 余姚市| 息烽县| 应用必备| 灵川县| 东光县| 福建省| 大荔县| 岢岚县| 武胜县| 当雄县| 鄂州市| 谢通门县| 和政县| 铜川市| 大宁县| 渑池县| 贡嘎县| 平陆县| 策勒县|