What does char 160 mean in my source code?

前端 未结 8 1052
温柔的废话
温柔的废话 2021-02-05 00:01

I am formatting numbers to string using the following format string \"# #.##\", at some point I need to turn back these number strings like (1 234 567) into something like 1234

8条回答
  •  一整个雨季
    2021-02-05 00:29

    This is a fast (and fairly readable) way of removing any characters classified as white space using Char.IsWhiteSpace:

    StringBuilder sb = new StringBuilder (value.Length);
    foreach (char c in value)
    {
        if (!char.IsWhiteSpace (c))
            sb.Append (c);
    }
    string value= sb.ToString();
    

    As dbemerlin points out, if you know you will only need numbers from your data, you would be better use Char.IsNumber or the even more restrictive Char.IsDigit:

    StringBuilder sb = new StringBuilder (value.Length);
    foreach (char c in value)
    {
        if (char.IsNumber(c))
            sb.Append (c);
    }
    string value= sb.ToString();
    

    If you need numbers and decimal seperators, something like this should suffice:

    StringBuilder sb = new StringBuilder (value.Length);
    foreach (char c in value)
    {
        if (char.IsNumber(c)|c == System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator )
            sb.Append (c);
    }
    string value= sb.ToString();
    

提交回复
热议问题