Fastest way to remove white spaces in string

后端 未结 13 1206
粉色の甜心
粉色の甜心 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 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+", "")
    
    }
    

提交回复
热议问题