我有一個這樣的字串:
string myString = "String testing string replace"
我想取那個字串并給字串這個詞一個樣式。所以最后的字串將是:
myString = "<span class='myclass'>String</span> testing <span class='myclass'>string</span> replace"
我怎樣才能以編程方式做到這一點?以便單詞string 的兩個實體都保持大小寫?
uj5u.com熱心網友回復:
也許是這樣的:
var myString2 = Regex.Replace(
myString,
Regex.Escape("string"),
"<span class='myclass'>$0</span>",
RegexOptions.IgnoreCase
);
模式只是您要替換的字串,因此如果其中包含任何對 Regex 有意義的字符(如句點),它們將完全匹配。當匹配發生時,整個匹配存盤在變數 $0 中。這個變數可以在替換中使用,所以當我們指定 IgnoreCase 時,它??意味著第一個匹配是 on String,這意味著替換 $0 是String.. 第二個匹配是string
uj5u.com熱心網友回復:
string myString = "String testing string replace";
string stringOut = "";
foreach (var word in myString.Split(new char[] { ' '}))
{
stringOut = $"<span class='myclass'>{word}</span> ";
}
Console.WriteLine(stringOut);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/365002.html
上一篇:System.Diagnostics.TraceSource不向ApplicationInsights發送資料
下一篇:如何使用自動回發?
