首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >代码审查: CLR RegexSubstring

代码审查: CLR RegexSubstring
EN

Stack Overflow用户
提问于 2010-05-13 05:13:55
回答 1查看 171关注 0票数 2

这还能更好吗? SQL Server2005的.NET 2.0兼容性:

代码语言:javascript
复制
public static SqlString RegexSubstring(SqlString regexpattern, 
                                       SqlString sourcetext, 
                                       SqlInt32 start_position)
{
   SqlString result = null;

   if (!regexpattern.IsNull && !sourcetext.IsNull && !start_position.IsNull)
   {
      int start_location = (int)start_position >= 0 ? (int)start_position : 0;

      Regex RegexInstance = new Regex(regexpattern.ToString());
      result = new SqlString(RegexInstance.Match(sourcetext.ToString(), 
                                                 start_location).Value);
   }

   return result;
}

这是我第一次尝试为SQL Server编写CLR函数/etc-参数一定要使用SqlString/etc数据类型吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-05-13 05:19:19

只需通过Refactor/Pro运行它

给出了这个:

代码语言:javascript
复制
public static SqlString RegexSubstring(SqlString regexpattern,
                               SqlString sourcetext,
                               SqlInt32 start_position) {
    if (regexpattern.IsNull || sourcetext.IsNull || start_position.IsNull)
        return null;

    Regex RegexInstance = new Regex(regexpattern.ToString());

    return new SqlString(RegexInstance.Match(sourcetext.ToString(),
                                               (int)start_position).Value);
}

请注意,start_location未使用,因此您可能忽略了警告?

另一件事只是一个风格问题,但是函数可以编写成不依赖于SqtTypes吗?然后代码变成:

代码语言:javascript
复制
    private static string RegexSubstring(string regexpattern, string sourcetext, int start_position) {

        if (regexpattern == null || sourcetext == null || start_position == null)
            return null;

        Regex RegexInstance = new Regex(regexpattern);
        return RegexInstance.Match(sourcetext, start_position).Value;
    }

并使用以下命令调用它:

代码语言:javascript
复制
new SqlString(RegexSubstring(regexpattern.ToString(), sourcetext.ToString(), start_position))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2822800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档