3 回答

TA貢獻1854條經驗 獲得超8個贊
根據你想要的,會有不同的方法,你可以使用 IndexOf 和 SubString,正則表達式也是一個解決方案。
// 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貢獻1155條經驗 獲得超0個贊
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貢獻1779條經驗 獲得超6個贊
您可以使用這個通用正則表達式。
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”標簽及其之間的內容。該Trim()調用是在替換后刪除字符串開頭/結尾處的所有空格。
- 3 回答
- 0 關注
- 185 瀏覽
添加回答
舉報