如何正确表示空格字符 [英] How to correctly represent a whitespace character

查看:1188
本文介绍了如何正确表示空格字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在C#中表示空白字符。我发现空字符串表示形式 string.Empty

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?

我想做这样的事情吗?

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


推荐答案

哪个空格字符?空字符串非常明确-由0个字符组成。但是, \t \n 都是包含单个字符的字符串,这些字符的特征是空格。

Which whitespace 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.

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

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.

编辑:现在,您已经解释了要执行的操作(应该这样做)。一直以来都是您的问题),最好使用 Regex。使用正则表达式 \s 拆分 表示空白:

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.Split with a regular expression of \s which represents whitespace:

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

请参见正则表达式字符类文档,以获取有关其他字符类的更多信息。

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

这篇关于如何正确表示空格字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆