C# 我的源代码中的 char 160 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2132348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:44:42  来源:igfitidea点击:

What does char 160 mean in my source code?

c#.net

提问by gyurisc

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 1234567. I am trying to strip out the empty chars but found that

我正在使用以下格式字符串“##.##”将数字格式化为字符串,在某些时候我需要将这些数字字符串(如(1 234 567))转回类似 1234567 的内容。我试图去除空字符但发现

value = value.Replace(" ", "");  

for some reason and the string remain 1 234 567. After looking at the string I found that

出于某种原因,字符串仍然是 1 234 567。在查看字符串后,我发现

value[1] is 160.

值 [1] 是 160。

I was wondering what the value 160 means?

我想知道值 160 是什么意思?

采纳答案by Jon Skeet

The answer is to look in Unicode Code Charts- where you'll find the Latin-1 supplement chart; this shows that U+00A0 (160 as per your title, not 167 as per the body) is a non-breaking space.

答案是查看Unicode Code Charts- 您可以在其中找到Latin-1 补充图表;这表明 U+00A0(根据您的标题为 160,而不是根据正文为 167)是一个不间断空格。

回答by YOU

char code 160would be  

字符代码160将是 

回答by Rubens Farias

Maybe you could to use a regex to replace those empty chars:

也许您可以使用正则表达式来替换那些空字符:

Regex.Replace(input, @"\p{Z}", "");

This will remove "any kind of whitespace or invisible separator".

这将删除“任何类型的空白或不可见分隔符”

回答by Colin Pickard

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

这是使用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 dbemerlinpoints out, if you know you will only need numbers from your data, you would be better use Char.IsNumberor the even more restrictive Char.IsDigit:

正如dbemerlin指出的那样,如果您知道只需要数据中的数字,则最好使用Char.IsNumber或更严格的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();

回答by Imran Athar

value.Replace(Convert.ToChar(160).ToString(),"")

value.Replace(Convert.ToChar(160).ToString(),"")

回答by Edward Olamisan

I would suggest using the char overload version:

我建议使用char 重载版本

value = value.Replace(Convert.ToChar(160), ' ') 

回答by Petr Voborník

Solution with extended methods:

扩展方法的解决方案:

public static class ExtendedMethods
{
    public static string NbspToSpaces(this string text)
    {
        return text.Replace(Convert.ToChar(160), ' ');
    }
}

And it can be used with this code:

它可以与以下代码一起使用:

value = value.NbspToSpaces();

回答by Boosa

Wouldn't be the preferred method to replace all empty characters (and this is what the questioner wanted to do) with the Regex Method which Rubens already posted?

用鲁本斯已经发布的正则表达式方法替换所有空字符(这就是提问者想要做的)不是首选方法吗?

Regex.Replace(input, @"\p{Z}", "");

or what Expresso suggests:

或 Expresso 建议:

Regex.Replace(input, @"\p{Zs}", "");

The difference here is that \p{Z} replaces any kind of whitespace or invisible separator whereas the \p{Zs} replaces a whitespace character that is invisible, but does take up space. You can read it here (Section Unicode Categories):

这里的区别在于 \p{Z} 替换任何类型的空白或不可见分隔符,而 \p{Zs} 替换不可见但确实占用空间的空白字符。您可以在此处阅读(Unicode 类别部分):

http://www.regular-expressions.info/unicode.html

http://www.regular-expressions.info/unicode.html

Using RegEx has the advantage that only one command is needed to replace also the normal whitespaces and not only the non-breaking space like explained in some answers above.

使用 RegEx 的优点是只需要一个命令来替换正常的空格,而不仅仅是上面一些答案中解释的不间断空格。

If performance is the way to go then of course other methods should be considered but this is out of scope here.

如果性能是要走的路,那么当然应该考虑其他方法,但这超出了这里的范围。