Fastest way to remove white spaces in string

后端 未结 13 1214
粉色の甜心
粉色の甜心 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:52

    With linq you can do it simply:

    emailaddress = new String(emailaddress
                                         .Where(x=>x!=' ' && x!='\r' && x!='\n')
                                         .ToArray());
    

    I didn't compare it with stringbuilder approaches, but is much more faster than string based approaches. Because it does not create many copy of strings (string is immutable and using it directly causes to dramatically memory and speed problems), so it's not going to use very big memory and not going to slow down the speed (except one extra pass through the string at first).

提交回复
热议问题