TheDeveloperBlog.com


C# char.IsSeparator Example

Char.IsSeparator. What does the char.IsSeparator method do? Is it useful for any specific tasks in your C# programs? IsSeparator refers to the Unicode concept of separator characters, which separate words.


Example. The simplest overload of char.IsSeparator receives one argument: a char. It then returns whether or not the char is a separator. A space is a separator. Other kinds of separators such as periods are not separators in Unicode.

Next: The program further outputs all the possible separator chars. Most of them cannot be displayed in the console.

Console
C# program that uses char.IsSeparator

using System;

class Program
{
    static void Main()
    {
	Console.WriteLine(char.IsSeparator(' '));
	Console.WriteLine(char.IsSeparator('.'));

	for (char c = char.MinValue; c < char.MaxValue; c++)
	{
	    if (char.IsSeparator(c))
	    {
		Console.WriteLine("{0} {1}", c, (int)c);
	    }
	}
    }
}

Output

True
False
  32
  160
? 5760
? 6158
  8192
  8193
  8194
  8195
  8196
  8197
  8198
? 8199
? 8200
? 8201
? 8202
? 8232
? 8233
? 8239
? 8287
  12288


Paths. File paths also have a concept of separators. These are not the same kind of separators as are described in Unicode. If you want to detect path separators, do not use char.IsSeparator.

Path

Summary. The separators detected by char.IsSeparator are whitespace characters that separate words. The most common separator is the space character. In many programs, testing against the space directly would be better than using char.IsSeparator.

Tip: In additional cases, the char.IsWhitespace method (covered in a separate article) is best.

char.IsWhiteSpace