How to improve the Replace function in CLR functio

2019-09-13 23:08发布

I have a CLR function to replace string values. but it runs a long time. How can I improve this function and make it runs faster?

    public static SqlString ReplaceValues(String Value)
    {
    // Put your code here
    char[] c = new char[] { '.' };
    Value= Value.Trim();
    Value = Value.Trim(c);
    Value = Value.Replace("'", "").Replace(")", " ").Replace("(", " ").Replace("-", " ").Replace("_", " ");

    Value = Regex.Replace(Value, @"[י", "+[י");
    Value = Regex.Replace(Value, @"\s+", " ");

    return new SqlString(Value.Trim());

   }

EDIT: I changed my function to use value.Replace, it's better, but still it runs more time than expected:

     public static SqlString ReplaceStreetValues(String Value)
    {
    // Put your code here
    Value = Value.Trim();
    char[] c = new char[]{'.'}; 
    Value = Value.Trim(c);

    Value= Value.Replace("'", "").Replace(")", " ").Replace("(", " ").Replace("-", " ").Replace("_", " ").Replace("רח", "");
   while (Value.IndexOf("  ")!=-1)
        Value = Value.Replace("  ", " ");
   while (Value.IndexOf("hh") !=-1)        
        Value = Value.Replace("hh", "h");

    return new SqlString(Value.Trim());

    }

thanks!!!

1条回答
ゆ 、 Hurt°
2楼-- · 2019-09-13 23:38

Try to use StringBuilder.Replace instead.

Should notably improve performance.

This is valid like a string.Replace(..) substitude and not for regex calls. But apparently the bottleneck is in string calls.

EDIT:

Example (pesudocode):

char[] c = new char[]{'.', ' '}; 
Value = Value.Trim(c);
var sb = new StringBuilder(Value);   

sb.Replace("'", "");
sb.Replace(")", " ");
sb.Replace("(", " ");
sb.Replace("-", " ");
sb.Replace("_", " ");
sb.Replace("רח", "");
查看更多
登录 后发表回答