Methods of Character Wrapper Class

The Character wrapper class has many useful methods. We will look at some of them.

boolean isDigit(char ch)

The method isDigit() accepts a char and checks if it is a digit. It will return true only if ch is a digit i.e. ch should be one of the following – ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’

Given below is an example.

public class Test {

    public static void main(String[] a) {
        boolean b1 = Character.isDigit('5');
        boolean b2 = Character.isDigit('-');
        System.out.println(b1);
        System.out.println(b2);
    }

}

Output

true
false

boolean isLetter(char ch)

This method returns true if ch is a letter. It can be either an uppercase letter – ‘A’, ‘B’, …. ‘Z’ or a lowercase letter.

Given below is an example program.

public class Test {

    public static void main(String[] a) {
        boolean b1 = Character.isLetter('s');
        boolean b2 = Character.isLetter('R');
        boolean b3 = Character.isLetter('&');
        System.out.println(b1);
        System.out.println(b2);
        System.out.println(b3);
    }

}

Output

true
true
false

boolean isLetterOrDigit(char ch)

Returns true if ch is either a letter or a digit i.e. ch can be ‘A’, ‘B’, … ‘Z’, ‘a’, ‘b’, … ‘z’, ‘0’, ‘1’, … ‘9’

An example program is given below.

public class Test {

    public static void main(String[] a) {
        boolean b1 = Character.isLetterOrDigit('s');
        boolean b2 = Character.isLetterOrDigit('9');
        boolean b3 = Character.isLetterOrDigit('&');
        System.out.println(b1);
        System.out.println(b2);
        System.out.println(b3);
    }

}

Output

true
true
false

boolean isLowerCase(char ch)

Returns true if ch is a lower case letter i.e. ch is one of ‘a’, ‘b’, … ‘z’. If ch is an uppercase letter or not a letter, then it returns false.

Example program is given below.

public class Test {

    public static void main(String[] a) {
        boolean b1 = Character.isLowerCase('s');
        boolean b2 = Character.isLowerCase('R');
        boolean b3 = Character.isLowerCase('&');
        System.out.println(b1);
        System.out.println(b2);
        System.out.println(b3);
    }

}

Output

true
false
false

boolean isUpperCase(char ch)

This method is similar to the previous method except that it returns true if ch is an uppercase letter.

public class Test {

    public static void main(String[] a) {
        boolean b1 = Character.isUpperCase('s');
        boolean b2 = Character.isUpperCase('R');
        boolean b3 = Character.isUpperCase('&');
        System.out.println(b1);
        System.out.println(b2);
        System.out.println(b3);
    }

}

Output

false
true
false

boolean isWhiteSpace(char ch)

A whitespace can be a space, tab or a new line. (There are few other characters which qualify as whitespace characters but we will not look at them here). If ch is a whitespace character, then this method returns true. Else, it returns false.

public class Test {

    public static void main(String[] a) {
        boolean b1 = Character.isWhitespace(' ');
        boolean b2 = Character.isWhitespace('\t');
        boolean b3 = Character.isWhitespace('\n');
        boolean b4 = Character.isWhitespace('3');
        System.out.println(b1);
        System.out.println(b2);
        System.out.println(b3);
        System.out.println(b4);
    }

}

Output

true
true
true
false

char toLowerCase(char ch)

This method takes a char and returns its lowercase equivalent. For example, if ch is ‘A’, then this method returns ‘a’. If ch is already in lowercase, then it returns the same lowercase letter without making any modifications. If ch is not a letter, it returns the same character without making any changes. No error will occur.

public class Test {

    public static void main(String[] a) {
        char c1 = Character.toLowerCase('s');
        char c2 = Character.toLowerCase('R');
        char c3 = Character.toLowerCase('&');
        System.out.println(c1);
        System.out.println(c2);
        System.out.println(c3);
    }

}

Output

s
r
&

char toUpperCase(char ch)

This is similar to the above method except that it converts ch to uppercase.

public class Test {

    public static void main(String[] a) {
        char c1 = Character.toUpperCase('s');
        char c2 = Character.toUpperCase('R');
        char c3 = Character.toUpperCase('&');
        System.out.println(c1);
        System.out.println(c2);
        System.out.println(c3);
    }

}

Output

S
R
&

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>