SoFunction
Updated on 2025-03-08

How to determine whether it is 26 English letters

Scene

It is necessary to determine whether the first letter is an English letter.

Some people say, isn't that simple? () can be done. The author thinks so, but the result is not the case. Code:

("character:'{}' Is it a letter or not:{}","I",("I")); // true
("character:'z' Is it a letter or not:{}","z",("z")); // true

After studying on Baidu, I found that isAlpha is a letter that is as long as it is in the letter range, and the range is much larger than the range of 26 letters.

Solution

Make regular judgments

Just use (reg) to judge, code:

String english="[a-zA-Z]"; 
("character:'{}' Is it a letter or not:{}","I","I".matches(english)); // true
("character:'{}' Is it a letter or not:{}","z","z".matches(english)); // true

Character list, then traverse

You can also write an array containing 26 characters and then encapsulate it as your own judgment method. But it feels not as convenient as matches, so I will not explain it in detail here.

Supplement: Java determines whether strings are all composed of numbers, upper and lower case letters, and special symbols

Directly upload the code and verify it.

package javaTest; 
import ;
import ; 
public class test { 
	/***
	  *
	  * @param args
	  * Java determines whether characters are all composed of numbers, lowercase letters, uppercase letters, and special symbols
	  * Regular expression judgment
	  */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String str = "aksjfjJAJIFJSL12455432^&^&(";
		String str1 = "abcdfjijgiaj";
		String str2 = "JAOGVNDIJEIO";
		String str3 = "12345465870";
		String str4 = "@#$^^%^&*(";		
		
		Pattern pattern1 = ("[a-z]*");
		Pattern pattern2 = ("[A-Z]*");
		Pattern pattern3 = ("[0-9]*");
		Pattern pattern4 = ("\\p{Punct}+");
		
		Matcher matcher1 = (str1);
		Matcher matcher2 = (str2);
		Matcher matcher3 = (str3);
		Matcher matcher4 = (str4);		
		
		if (()) {
			("All lowercase letters");
		}
		else {
			("Not all lowercase letters");
		}		
		
		if (()) {
			("All capital letters");
		}
		else {
			("Not all capital letters");
		}		
		
		if (()) {
			("It's all numbers");
		}
		else {
			("Not all numbers");
		}		
		
		if (()) {
			("It's all special symbols");
		}
		else {
			("Not all special symbols");
		}		
		
		//Judge whether the string is all composed of numbers, Java comes with the method to judge		boolean flag = true;
		for (int i = ()-1; i >= 0; i--) {
			if (!((i))) {
				flag = false;
			}
		}
		if (flag) {
			(str3 + "All numbers");
		} 
	} 
}

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.