鸟语天空
StringHelper
post by:追风剑情 2025-6-15 14:31
using System;
using System.Text.RegularExpressions;
/// <summary>
/// 字符串处理辅助类
/// </summary>
public sealed class StringHelper
{
    // 去除代码注释
    public static string StripCodeComment(string code)
    {
        //删除单行注释
        //(.*?): 非贪婪匹配任意字符(除了换行符)
        //$: 匹配行尾
        string patternSingleLine = @"//.*?$";
        code = Regex.Replace(code, patternSingleLine, string.Empty, RegexOptions.Multiline);

        //删除多行注释
        string patternMultiLine = @"/\*(.|\n)*?\*/";
        code = Regex.Replace(code, patternMultiLine, string.Empty, RegexOptions.Multiline);

        return code;
    }
}
评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容