BlackWaspTM
.NET Framework
.NET 1.1+

Character Testing with the Char Structure (2)

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.

IsLetter

The IsLetter method allows you to test whether a character is a letter. Digits, punctuation and white space are not classified as letters but letters from non-English alphabets are. Modify the line within the Load event handler's loop as follows to see the set of letter characters. On executing the program you should see several tens of thousands of results.

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

IsLetterOrDigit

The IsLetterOrDigit method provides a combination of the above two methods. This member returns true for any letter or digit character.

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

IsNumber

The IsNumber method differs from IsDigit because it includes characters that represent numbers that are not single digits. Number characters include fraction symbols, subscript and superscript numbers, digits embedded within symbols and roman numerals.

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

IsLower

The IsLower method returns true for characters that are designated as lower case letters. This method returns false for upper case letters and those that are considered as neither upper case nor lower case.

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

IsUpper

The IsUpper method is used to detect upper case letters. As with IsLower, letters that do not have a designated case generate a false result.

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

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