c# - 删除开始和结束之间的字符串的一部分
问题描述
先上代码:
string myString = "<at>onePossibleName</at> some question here regarding <at>disPossibleName</at>"
// some code to handle myString and save it in myEditedString
Console.WriteLine(myEditedString);
//output now is: some question here regarding <at>disPossibleName</at>
我想<at>onePossibleName</at>
从 myString 中删除。字符串onePossibleName
和disPossbileName
可以是任何其他字符串。
到目前为止,我正在与
string myEditedString = string.Join(" ", myString.Split(' ').Skip(1));
这里的问题是 ifonePossibleName
变成one Possible Name
.
尝试myString.Remove(startIndex, count)
也是如此 - 这不是解决方案。
解决方案
根据您的需要,会有不同的方法,您可以使用 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
推荐阅读
- node.js - 使用节点js的一个用户单设备登录
- java - 在listview中多次从db中获取最后一个值
- javascript - 如何制作工具提示,带边框的左箭头
- c - 如何在C中组合3个二进制数据?
- java - 当我们有多个表时获取 Querydsl Oracle Rowid
- python - 使用代理时请求响应发生变化(浏览器没有变化)
- c# - 如何使用 .net mvc 在 twilio 中处理我的短信状态回调?
- google-cloud-platform - 服务帐号权限
- javascript - 如何使用量角器中的函数重用另一个 js 文件中的代码?
- jenkins - 如何将 Jenkins 限制为仅在我的 Github 组织下进行回购?