3 回答

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊
根據(jù)你想要的,會(huì)有不同的方法,你可以使用 IndexOf 和 SubString,正則表達(dá)式也是一個(gè)解決方案。
// SubString and IndexOf method
// Usefull if you don't care of the word in the at tag, and you want to remove the first at tag
if (myString.Contains("</at>"))
{
var myEditedString = myString.Substring(myString.IndexOf("</at>") + 5);
}
// Regex method
var stringToRemove = "onePossibleName";
var rgx = new Regex($"<at>{stringToRemove}</at>");
var myEditedString = rgx.Replace(myString, string.Empty, 1); // The 1 precise that only the first occurrence will be replaced

TA貢獻(xiàn)1155條經(jīng)驗(yàn) 獲得超0個(gè)贊
string myString = "<at>onePossibleName</at> some question here regarding <at>disPossibleName</at>"
int sFrom = myString.IndexOf("<at>") + "<at>".Length;
int sTo = myString.IndexOf("</at>");
string myEditedString = myString.SubString(sFrom, sFrom - sTo);
Console.WriteLine(myEditedString);
//output now is: some question here regarding <at>disPossibleName</at>

TA貢獻(xiàn)1779條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以使用這個(gè)通用正則表達(dá)式。
var myString = "<at>onePossibleName</at> some question here regarding <at>disPossibleName</at>";
var rg = new Regex(@"<at>(.*?)<\/at>");
var result = rg.Replace(myString, "").Trim();
這將刪除所有“at”標(biāo)簽及其之間的內(nèi)容。該Trim()調(diào)用是在替換后刪除字符串開頭/結(jié)尾處的所有空格。
- 3 回答
- 0 關(guān)注
- 183 瀏覽
添加回答
舉報(bào)