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 represents a whitespace character?

I would like to do something like this:

test.ToLower().Split(string.Whitespace)
//test.ToLower().Split(Char.Whitespace)
Luke101
  • 63,072
  • 85
  • 231
  • 359
  • 2
    wouldn't it just be a single space? (or the ascii equivilant?) – Limey Jun 13 '12 at 16:49
  • 2
    Whitespace is a set of characters. – Brian Rasmussen Jun 13 '12 at 16:50
  • Possible duplicate of [Best way to specify whitespace in a String.Split operation](http://stackoverflow.com/questions/6111298/best-way-to-specify-whitespace-in-a-string-split-operation) – nevermind Sep 06 '16 at 12:50
  • 2
    a white space character is not just a space, accoording to https://msdn.microsoft.com/en-us/library/e9a023cx.aspx `Space, tab, linefeed, carriage-return, formfeed, vertical-tab, and newline characters are called "white-space characters" because they serve the same purpose as the spaces between words and lines on a printed page — they make reading easier` – Smith Dec 06 '16 at 10:40
  • `' '` 10 more to go... – huang May 17 '22 at 20:52

7 Answers7

36

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Hi, I am refering to this post http://stackoverflow.com/questions/6111298/best-way-to-specify-whitespace-in-a-string-split-operation. As I had the same problem. I have a csv file where the space was not matching ' ' in the split function. I would like to put string.Whitespace in the function if one exists. – Luke101 Jun 13 '12 at 16:57
  • @Luke101: It would have been useful if you'd said all of this to start with. See my edit - basically you should use regular expressions. – Jon Skeet Jun 13 '12 at 17:06
  • Perfect, exactly what I need. Sorry for the misunderstanding. Will clarify posts in the future. – Luke101 Jun 13 '12 at 17:21
10

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));
David
  • 239
  • 3
  • 10
9

No, there isn't such constant.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
2

So I had the same problem so what I did was create a string with a white space and just index the character.

String string = "Hello Morning Good Night";

char empty =  string.charAt(5);

Now whenever I need a empty character I will pull it from my reference in memory.

elena.kim
  • 930
  • 4
  • 12
  • 22
1

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

Tim S.
  • 55,448
  • 7
  • 96
  • 122
  • I used 'SPACE' instead of "SPACE" to represent space character in c# – Zeeshanef Jun 06 '14 at 14:45
  • 7
    `' '` is a `char`, `" "` is a `string`. In the context of `Split`, the `char` is probably what you want to use. So yeah, you're right – Tim S. Jun 06 '14 at 15:05
1

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

MSDN Reference

Mark Peters
  • 17,205
  • 2
  • 21
  • 17
1

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

var space = "\u0020"
Siarhei Kavaleuski
  • 1,450
  • 14
  • 15