BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET Framework
.NET 1.1+

Character Testing with the Char Structure

When developing software that may be deployed in many countries, it is important to be able to determine the type of a character according to the rules of the local culture and language. The Char structure provides methods to simplify this process.

Checking for Symbols and White Space

IsPunctuation

The IsPunctuation method allows you to check if a character is a valid punctuation mark. All other characters cause a false result.

if (char.IsPunctuation(c)) CharacterList.Items.Add(c);

IsSymbol

The IsSymbol method returns true for a much wider set of characters than IsPunctuation. The matching characters include English symbols, technical, mathematical and geometric symbols, arrows, Braille characters and dingbats.

if (char.IsSymbol(c)) CharacterList.Items.Add(c);

IsSeparator

The IsSeparator method is used to detect separators such as spaces, line breaks and paragraph marks. The sample code below will execute this method. However, as most separators are non-printing characters, the output will be minimal.

if (char.IsSeparator(c)) CharacterList.Items.Add(c);

IsWhiteSpace

The final method that we will examine in this article is named "IsWhiteSpace". As the name suggests, the method is used to test for white space characters. These include spaces of varying sizes, character separators, tab separators, line breaks and paragraph marks.

if (char.IsWhiteSpace(c)) CharacterList.Items.Add(c);
31 October 2009