Remove all whitespace from C# string with regex

前端 未结 7 1575
遇见更好的自我
遇见更好的自我 2020-12-25 10:18

I am building a string of last names separated by hyphens. Sometimes a whitespace gets caught in there. I need to remove all whitespace from end result.

Sample strin

7条回答
  •  甜味超标
    2020-12-25 10:47

    No need for regex. This will also remove tabs, newlines etc

    var newstr = String.Join("",str.Where(c=>!char.IsWhiteSpace(c)));
    

    WhiteSpace chars : 0009 , 000a , 000b , 000c , 000d , 0020 , 0085 , 00a0 , 1680 , 180e , 2000 , 2001 , 2002 , 2003 , 2004 , 2005 , 2006 , 2007 , 2008 , 2009 , 200a , 2028 , 2029 , 202f , 205f , 3000.

提交回复
热议问题