Java. Class Character. Class methods determine whether a symbol belongs to a given category

Class Character. Class methods determine whether a symbol belongs to a given category. Methods isDigit(), isIdentifierIgnorable(), isLowerCase(), isSpace(), isUpperCase(), isWhiteSpace()

This topic discusses methods that do not support Unicode code points.

1. Method isDigit(). Determines if the specified character is a digit

The isDigit() method determines whether the specified character is a digit. According to the documentation, the method declaration is as follows

public static boolean isDigit(char ch);

here

  • ch is the character under consideration. If ch is a digit, then the method returns true, otherwise the method returns false.

A character is considered a digit if the generic category type is DECIMAL_DIGIT_NUMBER. This value can be obtained by calling

Character.getType(ch)

where ch is a character checked for belonging to a given category.

Example.

// Method isDigit() - is the character a digit

// 1. Definition using the isDigit() method
boolean res;
char c = '5';
res = Character.isDigit(c); // res = true

c = 'z';
res = Character.isDigit(c); // res = false

res = Character.isDigit('+'); // res = false
res = Character.isDigit(';'); // res = false

// 2. Validation using the DECIMAL_DIGIT_NUMBER constant

// Determine if character '5' is a digit
int type = Character.getType('5');

if (type == Character.DECIMAL_DIGIT_NUMBER)
  res = true;
else
  res = false;

 

2. Method isIdentifierIgnorable(). Determines whether a character is ignored

The isIdentifierIgnorable() method determines whether a given character should be treated as ignored according to a Java identifier or Unicode identifier. According to the documentation, the syntax for declaring a method is

public static boolean isIdentifierIgnorable(char ch);

here

  • ch is the character to be checked.

The method returns true if the character is an ignored character that can be part of a Java or Unicode identifier. Otherwise, the method returns false.

The following characters are ignored in Java identifier and Unicode identifier:

  • ISO control characters that are not spaces;
  • characters with codes from ‘\u0000’ to ‘\u0008’;
  • characters with codes from ‘\u000E’ to ‘\u001B’;
  • characters with codes from ‘\u007F’ to ‘\u009F’;
  • all symbols that have values of the general category FORMAT.

Example.

// Using isIdentifierIgnorable() method
boolean res;
char c = '5';
res = Character.isIdentifierIgnorable(c); // res = false

c = '\u0003';
res = Character.isIdentifierIgnorable(c); // res = true

res = Character.isIdentifierIgnorable('\u0012'); // res = true

 

3. Method isLowerCase(). Is the specified character lowercase

The isLowerCase() method determines whether a character belongs to lowercase letters (a, b, c, …). The general syntax for declaring a method is:

public static boolean isLowerCase(char ch);

here

  • ch – the character being checked.

The method returns true if ch is a lowercase character.

A character is lowercase if its generic category type is LOWERCASE_LETTER. This type can be determined using the call

Character.getType(ch)

Example.

// Method isLowerCase() - is a character a lowercase character

// 1. Using the isLowerCase() method
boolean res;
char ch = '5';
res = Character.isLowerCase(ch); // res = false

res = Character.isLowerCase('+'); // res = false
res = Character.isLowerCase('f'); // res = true

// 2. Using the LOWERCASE_LETTER constant.
// Determine which category the character 'b' belongs to
byte categoryType = (byte)Character.getType('b');

if (categoryType == Character.LOWERCASE_LETTER)
  res = true;
else
  res = false;

 

4. Method isSpace(). Determining if the specified character is whitespace

The isSpace() method determines whether the specified character is an ISO-LATIN-1 space. The syntax for declaring a method is as follows:

public static boolean isSpace(char ch);

here ch is the character to check.

The method returns true for the following characters:

'\t' - tabulation
'\n' - new line
'\f' - Form Feed
'\r' - carriage return
' '  - space

Example.

// Method isSpace() - is there a whitespace character
boolean res;
char ch = '5';

res = Character.isSpace(ch); // res = false

res = Character.isSpace(' '); // res = true
res = Character.isSpace('\n'); // res = true

 

5. Method isUpperCase()

The isUpperCase() method is used to determine if a character is in uppercase. The general syntax for declaring a method is as follows

public static boolean isUpperCase(char ch);

here

  • ch – the character being checked.

The method returns true if the character ch is in uppercase. Otherwise, the method returns false.

The character is considered an uppercase character if the call

Character.getType(ch)

returns the UPPERCASE_LETTER value.

Example.

// The isUpperCase() method - is there an uppercase character

// 1. Using the isUpperCase() method
boolean res;
char ch = '5';
res = Character.isUpperCase(ch); // res = false

res = Character.isUpperCase('a'); // res = false
res = Character.isUpperCase('B'); // res = true
res = Character.isUpperCase('F'); // res = true

// 2. Using the UPPERCASE_LETTER constant
// Determine if there is an uppercase 'Z' character
if (Character.getType('Z')==Character.UPPERCASE_LETTER)
  res = true;
else
  res = false;

 

6. Method isWhiteSpace(). Determine if there is an uppercase character

The isWhiteSpace() method is an updated version of the isSpace() method and is designed to determine if a given character is whitespace according to Java interpretation.

The general form of the method is as follows

public static boolean isWhitespace(char ch);

here

  • ch – the character being checked.

The method returns true if the character ch is whitespace. Otherwise, the method returns false.

Compared to the isSpace() method, this method has additional criteria for detecting whitespace, taking into account the Unicode encoding.

Example.

// Method isWhiteSpace() - is there a whitespace character

boolean res;
char ch = '5';
res = Character.isWhitespace(ch);   // res = false

res = Character.isWhitespace('\t'); // res = true
res = Character.isWhitespace('\r'); // res = true