Zac*_*ach 3 c# string textbox code-comments
我有一个文本框,用户可以用脚本语言编辑文本.我已经想出如何让用户在一次点击中注释掉行,但似乎无法弄清楚如何正确地取消注释.例如,如果该框具有:
Run Code Online (Sandbox Code Playgroud)Normal Text is here More normal text -- Commented text -- More commented text Normal Text again --Commented Text Again
因此,当用户选择任意数量的文本并决定取消注释时,将从包含它的行的开头删除" - ".没有" - "的行应该不受影响.简而言之,我想要一个与Visual Studio中的类似的取消注释功能.有没有办法实现这个目标?
谢谢
Ati*_*ziz 10
用System.Text.RegularExpressions.Regex.Replace一个简单而强大的解决方案:
Regex.Replace(str, @"^--\s*", String.Empty, RegexOptions.Multiline)
Run Code Online (Sandbox Code Playgroud)
这是C#Interactive会话中的工作证明:
Microsoft (R) Visual C# Interactive Compiler version 1.2.0.60317
Copyright (C) Microsoft Corporation. All rights reserved.
Type "#help" for more information.
> using System.Text.RegularExpressions;
> var str = @"Normal Text is here
. More normal text
. -- Commented text
. -- More commented text
. Normal Text again
. --Commented Text Again";
> str = Regex.Replace(str, @"^--\s*", string.Empty, RegexOptions.Multiline);
> Console.WriteLine(str);
Normal Text is here
More normal text
Commented text
More commented text
Normal Text again
Commented Text Again
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14679 次 |
| 最近记录: |