thestringIwan-6ren">
gpt4 book ai didi

c# - 从字符串的开头和结尾删除 BR 标记

转载 作者:行者123 更新时间:2023-12-02 08:57:08 24 4
gpt4 key购买 nike

我如何使用类似的东西

return Regex.Replace("/(^)?(<br\s*\/?>\s*)+$/", "", source);

替换此案例:

<br>thestringIwant => thestringIwant
<br><br>thestringIwant => thestringIwant
<br>thestringIwant<br> => thestringIwant
<br><br>thestringIwant<br><br> => thestringIwant
thestringIwant<br><br> => thestringIwant

它可以在开头或结尾有多个 br 标签,但我不想删除中间的任何 br 标签。

最佳答案

几个循环可以解决问题,并且更容易阅读和理解(使用正则表达式=明天你看看自己的代码,想知道到底发生了什么)

while(source.StartsWith("<br>")) 
source = source.SubString(4);
while(source.EndsWith("<br>"))
source = source.SubString(0,source.Length - 4);

return source;

关于c# - 从字符串的开头和结尾删除 BR 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18717206/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com