在 C# 中使用正则表达式仅用下划线替换前导和尾随空格
我只想用下划线数量替换字符串的前导和尾随空格。
输入字符串
" New Folder "
(注:该字符串前面有一个空格,后面有两个空格)
输出
我想要的输出字符串 "_New Folder__"< /代码>
(输出字符串前面有一个下划线,后面有两个下划线。)
I want to replace only leading and trailing white space of a string by number of underscore.
Input String
" New Folder "
(Notes: There is one white space at front and two white spaces at the end of this string)
Output
My desire output string "_New Folder__"
(The output string has one underscore at the front and two underscore at the end.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种解决方案是使用回调:
或使用环视(有点棘手):
One solution is using a callback:
Or using lookaround (a bit trickier):
您也可以选择非正则表达式解决方案,但我不确定它是否漂亮:
You may also choose a non-regex solution, but I'm not sure it's pretty: