国产老熟女高潮毛片A片仙踪林,欧美喂奶吃大乳,狠狠爱无码一区二区三区,女神的私人医生动漫免费阅读

新聞建站cms系統、政府cms系統定制開發

廣州網站建設公司-閱速公司

asp.net新聞發布系統、報紙數字報系統方案
/
http://m.tjsimaide.com/
廣州網站建設公司
您當前位置:首頁>ASP.NET MVC

ASP.NET MVC

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

發布時間:2021/9/26 17:30:20  作者:Admin  閱讀:874  

廣告:

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

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(); ;//指定替換的范圍實現替換一次,并且指定范圍中要只有一個替換的字符串
      Console.WriteLine(str);//輸出:hello worldb cjavapy!!!
      Console.ReadKey();
    }
  }
}

2、使用正則表達式(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);//最后一個參數是替換的次數
      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);//指定替換的范圍實現替換一次,并且指定范圍中要只有一個替換的字符串
      Console.WriteLine(str);//輸出:hello worldb cjavapy!!!
      Console.ReadKey();
    }
  }
}

4、通過擴展方法實現ReplaceOne

擴展方法實現代碼

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;
}
}
}

調用代碼

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");//通過擴展方法替換
      Console.WriteLine(str);//輸出:hello worldb cjavapy!!!
      Console.ReadKey();
    }
  }
}

廣告:

相關文章
replace
cms新聞系統購買咨詢
掃描關注 廣州閱速軟件科技有限公司
掃描關注 廣州閱速科技
主站蜘蛛池模板: 万源市| 南汇区| 图们市| 贵定县| 浦东新区| 泰顺县| 宜宾县| 昌邑市| 浙江省| 汝阳县| 沅江市| 库尔勒市| 朝阳县| 北票市| 孟州市| 九寨沟县| 中西区| 泾阳县| 晴隆县| 绥宁县| 平度市| 大石桥市| 盐津县| 平远县| 玉山县| 扎赉特旗| 乐东| 洱源县| 凉城县| 中卫市| 孟村| 新疆| 新津县| 东山县| 榕江县| 衡水市| 德昌县| 尼勒克县| 苍南县| 隆安县| 泽库县|