Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly represent a whitespace character

Tags:

c#

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) 
like image 514
Luke101 Avatar asked Jun 13 '12 16:06

Luke101


1 Answers

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.

like image 132
Jon Skeet Avatar answered Sep 22 '22 23:09

Jon Skeet