How to correctly represent a whitespace character

后端 未结 6 1385
醉酒成梦
醉酒成梦 2020-12-15 16:31

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 represent

相关标签:
6条回答
  • 2020-12-15 16:48

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

    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));
    
    0 讨论(0)
  • 2020-12-15 16:50

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

    MSDN Reference

    0 讨论(0)
  • 2020-12-15 16:57

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

    var space = "\u0020"
    
    0 讨论(0)
  • 2020-12-15 17:00

    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.

    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.

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

    0 讨论(0)
  • 2020-12-15 17:01

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

    0 讨论(0)
  • 2020-12-15 17:12

    No, there isn't such constant.

    0 讨论(0)
提交回复
热议问题