C# 如何正确表示空白字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11019561/
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-09 16:08:42  来源:igfitidea点击:

How to correctly represent a whitespace character

c#

提问by Luke101

I wanted to know how to represent a whitespace character in C#. I found the empty string representation string.Empty. Is there anything like that that represents a whitespace character?

我想知道如何在 C# 中表示空白字符。我找到了空字符串表示string.Empty。有没有类似的东西代表空白字符?

I would like to do something like this:

我想做这样的事情:

test.ToLower().Split(string.Whitespace)
//test.ToLower().Split(Char.Whitespace)

采纳答案by Jon Skeet

Whichwhitespace character? The empty string is pretty unambiguous - it's a sequence of 0 characters. However, " ", "\t"and "\n"are all strings containing a single character which is characterized as whitespace.

哪个空白字符?空字符串非常明确 - 它是 0 个字符的序列。然而," ""\t""\n"是含一个字符其特点为空白的所有字符串。

If you just mean a space, use a space. If you mean some other whitespace character, there may well be a custom escape sequence for it (e.g. "\t"for tab) or you can use a Unicode escape sequence ("\uxxxx"). I would discourage you from including non-ASCII characters in your source code, particularly whitespace ones.

如果您只是指一个空格,请使用空格。如果您指的是其他一些空白字符,则很可能有一个自定义的转义序列(例如"\t"用于制表符),或者您可以使用 Unicode 转义序列 ( "\uxxxx")。我不鼓励您在源代码中包含非 ASCII 字符,尤其是空白字符。

EDIT: Now that you've explained what you want to do (which should have been in your question to start with) you'd be better off using Regex.Splitwith a regular expression of \swhich represents whitespace:

编辑:既然你已经解释了你想要做什么(这应该是你的问题开始),你最好使用代表空格Regex.Split的正则表达式\s

Regex regex = new Regex(@"\s");
string[] bits = regex.Split(text.ToLower());

See the Regex Character Classesdocumentation for more information on other character classes.

有关其他字符类的更多信息,请参阅Regex 字符类文档。

回答by Darin Dimitrov

No, there isn't such constant.

不,没有这样的常数。

回答by Tim S.

Which whitespace character? The most common is the normal space, which is between each word in my sentences. This is just " ".

哪个空白字符?最常见的是普通空格,它位于我句子中的每个单词之间。这只是" "

回答by Mark Peters

Using regular expressions, you can represent any whitespace character with the metacharacter "\s"

使用正则表达式,您可以用元字符“\s”表示任何空白字符

MSDN Reference

MSDN 参考

回答by David

The WhiteSpace CHAR can be referenced using ASCII Codes here.And Character# 32 represents a white space, Therefore:

可以在此处使用ASCII 代码引用 WhiteSpace CHAR 而 Character# 32 代表一个空格,因此:

char space = (char)32;

For example, you can use this approach to produce desired number of white spaces anywhere you want:

例如,您可以使用这种方法在您想要的任何地方生成所需数量的空格:

int _length = {desired number of white spaces}
string.Empty.PadRight(_length, (char)32));

回答by tscissors

You can always use Unicode character, for me personally this is the most clear solution:

您始终可以使用 Unicode 字符,对我个人而言,这是最清晰的解决方案:

var space = "\u0020"