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 关注
- 145 浏览
添加回答
举报