假设我有以下字符串:
string str = "<tag>text</tag>";
我想将“标签”更改为“newTag”,因此结果将是:
"<newTag>text</newTag>"
最好的方法是什么?
我尝试搜索 <[/]*tag> 但后来我不知道如何在我的结果中保留可选的 [/]...
最佳答案
为什么要使用正则表达式:
string newstr = str.Replace("tag", "newtag");
或
string newstr = str.Replace("<tag>","<newtag>").Replace("</tag>","</newtag>");
编辑@RaYell 的评论
关于c# - 如何在 C# 中替换字符串的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2522714/