Java проверить заглавные буквы

Checking Character Properties

You can categorize characters according to their properties. For instance, X is an uppercase letter and 4 is a decimal digit. Checking character properties is a common way to verify the data entered by end users. If you are selling books online, for example, your order entry screen should verify that the characters in the quantity field are all digits.

Developers who aren’t used to writing global software might determine a character’s properties by comparing it with character constants. For instance, they might write code like this:

char ch; //. // This code is WRONG! // check if ch is a letter if ((ch >= 'a' && ch = 'A' && ch = '0' && ch 

The preceding code is wrong because it works only with English and a few other languages. To internationalize the previous example, replace it with the following statements:

char ch; // . // This code is OK! if (Character.isLetter(ch)) // . if (Character.isDigit(ch)) // . if (Character.isSpaceChar(ch)) // .

The Character methods rely on the Unicode Standard for determining the properties of a character. Unicode is a 16-bit character encoding that supports the world's major languages. In the Java programming language char values represent Unicode characters. If you check the properties of a char with the appropriate Character method, your code will work with all major languages. For example, the Character.isLetter method returns true if the character is a letter in Chinese, German, Arabic, or another language.

The following list gives some of the most useful Character comparison methods. The Character API documentation fully specifies the methods.

  • isDigit
  • isLetter
  • isLetterOrDigit
  • isLowerCase
  • isUpperCase
  • isSpaceChar
  • isDefined

The Character.getType method returns the Unicode category of a character. Each category corresponds to a constant defined in the Character class. For instance, getType returns the Character.UPPERCASE_LETTER constant for the character A. For a complete list of the category constants returned by getType , see the Character API documentation. The following example shows how to use getType and the Character category constants. All of the expressions in these if statements are true :

if (Character.getType('a') == Character.LOWERCASE_LETTER) // . if (Character.getType('R') == Character.UPPERCASE_LETTER) // . if (Character.getType('>') == Character.MATH_SYMBOL) // . if (Character.getType('_') == Character.CONNECTOR_PUNCTUATION) // .

Источник

Java - Character isUpperCase() Method

The Java Character isUpperCase() method determines if a character is an uppercase character or not.

A character is said to be an uppercase character if its general category type is UPPERCASE_LETTER as provided by Character.getType(ch) method; or it has contributory property Other_Uppercase as defined by the Unicode Standards.

As per the generalized definition, in the uppercase written style, the characters are all present in their capital forms. For example, a character formed by combining basic Latin letters, "LJ" is in its uppercase style.

Syntax

Following is the syntax for Java Character isUpperCase() method

public static boolean isUpperCase(char ch) (or) public static boolean isUpperCase(int codePoint)

Parameters

  • ch − the character to be tested
  • codePoint − the Unicode code point to be tested

Return Value

This method returns true if the argument is uppercased, false otherwise.

Example

The following example shows the usage of Java Character isUpperCase(char ch) method.

package com.tutorialspoint; import java.lang.*; public class CharacterDemo < public static void main(String[] args) < // create 2 char primitives ch1, ch2 char ch1, ch2; // assign values to ch1, ch2 ch1 = 'K'; ch2 = '\u0e16'; // represents THAI CHARACTER THO THUNG // create 2 boolean primitives b1, b2 boolean b1, b2; // check if ch1, ch2 are uppercase and assign results to b1, b2 b1 = Character.isUpperCase(ch1); b2 = Character.isUpperCase(ch2); String str1 = ch1 + " is uppercase character is " + b1; String str2 = "ch2 is uppercase character is " + b2; // print b1, b2 values System.out.println( str1 ); System.out.println( str2 ); >>

Output

Let us compile and run the above program, this will produce the following result −

K is uppercase character is true ch2 is uppercase character is false

Example

The following example shows the usage of Java Character isUpperCase(int codePoint) method.

package com.tutorialspoint; import java.lang.*; public class CharacterDemo < public static void main(String[] args) < // create 2 int primitives cp1, cp2 int cp1, cp2; // assign values to cp1, cp2 cp1 = 0x004a; // represents J cp2 = 0x0071; // represents x // create 2 boolean primitives b1, b2 boolean b1, b2; // check if cp1, cp2 are uppercase and assign results to b1, b2 b1 = Character.isUpperCase(cp1); b2 = Character.isUpperCase(cp2); String str1 = "cp1 is uppercase character is " + b1; String str2 = "cp2 is uppercase character is " + b2; // print b1, b2 values System.out.println( str1 ); System.out.println( str2 ); >>

Output

Let us compile and run the above program, this will produce the following result −

cp1 is uppercase character is true cp2 is uppercase character is false

Example

We can also use loop statements on a character sequence to traverse through it and check how many uppercase characters are present in it using this method. If there are no uppercase letters present in the string, false is printed as the output.

import java.lang.*; public class CharacterDemo < public static void main(String[] args) < String s = "HeLLo WorLD"; int count = 0; for(int i = 0; i < s.length(); i++) < char ch = s.charAt(i); if(Character.isUpperCase(ch)) < System.out.println(ch + " is an Uppercase Letter"); count = count + 1; >> if(count == 0) System.out.println("No Uppercase Letters in the String"); > >

Output

The output of the program above will be displayed after compiling and running it as follows −

H is an Uppercase Letter L is an Uppercase Letter L is an Uppercase Letter W is an Uppercase Letter L is an Uppercase Letter D is an Uppercase Letter

Example

Another example makes use of loop statements to visit every character element in a character array and determine whether it is an uppercase letter.

import java.lang.*; public class CharacterDemo < public static void main(String[] args) < char c[] = ; int count = 0; for(int i = 0; i < c.length; i++) < if(Character.isUpperCase(c[i])) < System.out.println(c[i] + " is an Uppercase Letter"); count = count + 1; >else < System.out.println(c[i] + " is not an Uppercase Letter"); >> if(count == 0) System.out.println("\n\nNo Uppercase Letters in the String"); > >

Output

Let us compile and run the program given above, which will produce the output as follows −

a is not an Uppercase Letter B is an Uppercase Letter D is an Uppercase Letter f is not an Uppercase Letter r is not an Uppercase Letter

Источник

Class Character

The Character class wraps a value of the primitive type char in an object. An object of class Character contains a single field whose type is char .

In addition, this class provides a large number of static methods for determining a character's category (lowercase letter, digit, etc.) and for converting characters from uppercase to lowercase and vice versa.

Unicode Conformance

The fields and methods of class Character are defined in terms of character information from the Unicode Standard, specifically the UnicodeData file that is part of the Unicode Character Database. This file specifies properties including name and category for every assigned Unicode code point or character range. The file is available from the Unicode Consortium at http://www.unicode.org.

Character information is based on the Unicode Standard, version 15.0.

The Java platform has supported different versions of the Unicode Standard over time. Upgrades to newer versions of the Unicode Standard occurred in the following Java releases, each indicating the new version:

Shows Java releases and supported Unicode versions
Java release Unicode version
Java SE 20 Unicode 15.0
Java SE 19 Unicode 14.0
Java SE 15 Unicode 13.0
Java SE 13 Unicode 12.1
Java SE 12 Unicode 11.0
Java SE 11 Unicode 10.0
Java SE 9 Unicode 8.0
Java SE 8 Unicode 6.2
Java SE 7 Unicode 6.0
Java SE 5.0 Unicode 4.0
Java SE 1.4 Unicode 3.0
JDK 1.1 Unicode 2.0
JDK 1.0.2 Unicode 1.1.5

Variations from these base Unicode versions, such as recognized appendixes, are documented elsewhere.

Unicode Character Representations

The char data type (and therefore the value that a Character object encapsulates) are based on the original Unicode specification, which defined characters as fixed-width 16-bit entities. The Unicode Standard has since been changed to allow for characters whose representation requires more than 16 bits. The range of legal code points is now U+0000 to U+10FFFF, known as Unicode scalar value. (Refer to the definition of the U+n notation in the Unicode Standard.)

The set of characters from U+0000 to U+FFFF is sometimes referred to as the Basic Multilingual Plane (BMP). Characters whose code points are greater than U+FFFF are called supplementary characters. The Java platform uses the UTF-16 representation in char arrays and in the String and StringBuffer classes. In this representation, supplementary characters are represented as a pair of char values, the first from the high-surrogates range, (\uD800-\uDBFF), the second from the low-surrogates range (\uDC00-\uDFFF).

  • The methods that only accept a char value cannot support supplementary characters. They treat char values from the surrogate ranges as undefined characters. For example, Character.isLetter('\uD840') returns false , even though this specific value if followed by any low-surrogate value in a string would represent a letter.
  • The methods that accept an int value support all Unicode characters, including supplementary characters. For example, Character.isLetter(0x2F81A) returns true because the code point value represents a letter (a CJK ideograph).

In the Java SE API documentation, Unicode code point is used for character values in the range between U+0000 and U+10FFFF, and Unicode code unit is used for 16-bit char values that are code units of the UTF-16 encoding. For more information on Unicode terminology, refer to the Unicode Glossary.

This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail.

Источник

How to check if a character is uppercase in Java?

Few methods to check if a character is uppercase are discussed below.

Checking if a character is uppercase using the isUpperCase() method

The isUpperCase() method of the Character class determines if the specified character is an uppercase character.

public class Example < public static void main(String[] args) < char ch = 'K'; if (Character.isUpperCase(ch)) < System.out.println("Character is in Uppercase."); >else < System.out.println("Character is in Lowercase."); >> >
Character is in Uppercase.

Checking if a character is uppercase using ASCII value

The ASCII value of uppercase alphabets lies in the range of 65 - 91. Likewise, the range of lowercase alphabets is 97 - 122. If the given lies within this range, then the given character is an uppercase alphabet, else it will be a lowercase alphabet or special character.

char ch = 'N'; if (ch >= 65 && ch = 97 && ch 
The character is in Uppercase.

Checking if a character is uppercase using contains() method

To check if a character is uppercase using contains(), first we declare a string str with all the uppercase alphabets from A - Z. Then the given character is converted to string using Character.toString() method. This is done because the contains() method accepts a string parameter. Then we check if the converted string is present in the string str using contains() method. If it is present, then it returns else it returns false.

String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char ch = 'N'; String s = Character.toString(ch); boolean res = str.contains(s); if (res) System.out.println("The character is in Uppercase."); else
The character is in Uppercase.

Источник

Читайте также:  Font bold and italic in css
Оцените статью