Home  >  Article  >  Java  >  Use the isUpperCase() method of the Character class in Java to determine whether a character is an uppercase letter

Use the isUpperCase() method of the Character class in Java to determine whether a character is an uppercase letter

WBOY
WBOYOriginal
2023-07-26 13:09:181782browse

Use the isUpperCase() method of the Character class in Java to determine whether a character is an uppercase letter

In the Java programming language, we often need to determine whether a character is an uppercase letter. In order to realize this function, Java provides the Character class, whose isUpperCase() method can determine whether a character is an uppercase letter. This article will introduce the use of this method and sample code.

Using the isUpperCase() method of the Character class is very simple. You only need to pass the character to be determined as a parameter to the method, and it will return a Boolean value indicating whether the character is an uppercase letter. If the return value is true, it means that the character is an uppercase letter, otherwise it means that the character is not an uppercase letter.

The following is an example code that uses the isUpperCase() method to determine whether a character is an uppercase letter:

public class Main {
    public static void main(String[] args) {
        char ch = 'A';

        if (Character.isUpperCase(ch)) {
            System.out.println(ch + "是大写字母");
        } else {
            System.out.println(ch + "不是大写字母");
        }
    }
}

The above code defines a character variable ch, with the initial value being the uppercase letter 'A'. Then use the isUpperCase() method to determine whether ch is an uppercase letter. If so, print "ch is an uppercase letter", otherwise print "ch is not an uppercase letter".

Run the above code, the output result is: A is an uppercase letter. This is because 'A' is indeed a capital letter.

In addition to single characters, the isUpperCase() method can also be used to determine whether the characters in a string are uppercase letters. The following is a sample code:

public class Main {
    public static void main(String[] args) {
        String str = "Hello, World!";

        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (Character.isUpperCase(ch)) {
                System.out.println(ch + "是大写字母");
            }
        }
    }
}

The above code defines a string variable str with an initial value of "Hello, World!". Then use a for loop to iterate through each character in str, and use the isUpperCase() method to determine whether the character is an uppercase letter. If so, print "ch is an uppercase letter".

Run the above code, the output result is:
H is an uppercase letter
W is an uppercase letter

This is because the character 'H' in the string "Hello, World!" ' and 'W' are both capital letters.

To summarize, using the isUpperCase() method of the Character class can easily determine whether a character or a character in a string is an uppercase letter. Through this method, we can easily judge and process uppercase letters in Java programming. I hope the sample code in this article can help readers understand and apply the isUpperCase() method.

The above is the detailed content of Use the isUpperCase() method of the Character class in Java to determine whether a character is an uppercase letter. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn