Fastest way to remove white spaces in string

后端 未结 13 1116
粉色の甜心
粉色の甜心 2020-11-27 18:39

I\'m trying to fetch multiple email addresses seperated by \",\" within string from database table, but it\'s also returning me whitespaces, and I want to remove the whitesp

相关标签:
13条回答
  • 2020-11-27 18:58
    string input =Yourinputstring;
    string[] strings = input.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
    foreach (string value in strings)
    {
       string newv= value.Trim();
       if (newv.Length > 0)
       newline += value + "\r\n";
    }
    
    0 讨论(0)
  • 2020-11-27 18:58
    string s = " Your Text ";
    
    string new = s.Replace(" ", string.empty);
    
    // Output:
    // "YourText"
    
    0 讨论(0)
  • 2020-11-27 19:00

    I would build a custom extension method using StringBuilder, like:

    public static string ExceptChars(this string str, IEnumerable<char> toExclude)
    {
        StringBuilder sb = new StringBuilder(str.Length);
        for (int i = 0; i < str.Length; i++)
        {
            char c = str[i];
            if (!toExclude.Contains(c))
                sb.Append(c);
        }
        return sb.ToString();
    }
    

    Usage:

    var str = s.ExceptChars(new[] { ' ', '\t', '\n', '\r' });
    

    or to be even faster:

    var str = s.ExceptChars(new HashSet<char>(new[] { ' ', '\t', '\n', '\r' }));
    

    With the hashset version, a string of 11 millions of chars takes less than 700 ms (and I'm in debug mode)

    EDIT :

    Previous code is generic and allows to exclude any char, but if you want to remove just blanks in the fastest possible way you can use:

    public static string ExceptBlanks(this string str)
    {
        StringBuilder sb = new StringBuilder(str.Length);
        for (int i = 0; i < str.Length; i++)
        {
            char c = str[i];
            switch (c)
            {
                case '\r':
                case '\n':
                case '\t':
                case ' ':
                    continue;
                default:
                    sb.Append(c);
                    break;
            }
        }
        return sb.ToString();
    }
    

    EDIT 2 :

    as correctly pointed out in the comments, the correct way to remove all the blanks is using char.IsWhiteSpace method :

    public static string ExceptBlanks(this string str)
    {
        StringBuilder sb = new StringBuilder(str.Length);
        for (int i = 0; i < str.Length; i++)
        {
            char c = str[i];
            if(!char.IsWhiteSpace(c))
                sb.Append(c);
        }
        return sb.ToString();
    }
    
    0 讨论(0)
  • 2020-11-27 19:01

    You should consider replacing spaces on the record-set within your stored procedure or query using the REPLACE( ) function if possible & even better fix your DB records since a space in an email address is invalid anyways.

    As mentioned by others you would need to profile the different approaches. If you are using Regex you should minimally make it a class-level static variable:

    public static Regex MultipleSpaces = new Regex(@"\s+", RegexOptions.Compiled);
    

    emailAddress.Where(x=>{ return x != ' ';}).ToString( ) is likely to have function overhead although it could be optimized to inline by Microsoft -- again profiling will give you the answer.

    The most efficient method would be to allocate a buffer and copy character by character to a new buffer and skip the spaces as you do that. C# does support pointers so you could use unsafe code, allocate a raw buffer and use pointer arithmetic to copy just like in C and that is as fast as this can possibly be done. The REPLACE( ) in SQL will handle it like that for you.

    0 讨论(0)
  • 2020-11-27 19:05

    There are many diffrent ways, some faster then others:

    public static string StripTabsAndNewlines(this string str) {
    
        //string builder (fast)
        StringBuilder sb = new StringBuilder(str.Length);
        for (int i = 0; i < str.Length; i++) {
            if ( !  Char.IsWhiteSpace(s[i])) {
                sb.Append();
            }
        }
        return sb.tostring();
    
        //linq (faster ?)
        return new string(str.ToCharArray().Where(c => !Char.IsWhiteSpace(c)).ToArray());
    
        //regex (slow)
        return Regex.Replace(str, @"\s+", "")
    
    }
    
    0 讨论(0)
  • 2020-11-27 19:13
    emailaddress.Replace("  ", string.empty);
    
    0 讨论(0)
提交回复
热议问题