How to outperform this regex replacement?

Posted by spender on Stack Overflow See other posts from Stack Overflow or by spender
Published on 2010-04-27T10:16:01Z Indexed on 2010/04/27 10:23 UTC
Read the original article Hit count: 420

After considerable measurement, I have identified a hotspot in one of our windows services that I'd like to optimize. We are processing strings that may have multiple consecutive spaces in it, and we'd like to reduce to only single spaces. We use a static compiled regex for this task:

private static readonly Regex 
    regex_select_all_multiple_whitespace_chars = 
        new Regex(@"\s+",RegexOptions.Compiled);

and then use it as follows:

var cleanString=
    regex_select_all_multiple_whitespace_chars.Replace(dirtyString.Trim(), " ");

This line is being invoked several million times, and is proving to be fairly intensive. I've tried to write something better, but I'm stumped. Given the fairly modest processing requirements of the regex, surely there's something faster. Could unsafe processing with pointers speed things further?

© Stack Overflow or respective owner

Related posts about c#

Related posts about string-manipulation