繁体   English   中英

在Java中检查字符串中的字母

[英]Checking for Alphabets in a String in java

我有一个字符串“ BC + D * E-”。 我想检查字符串的每个字符是否是字母。 我尝试使用isLetter(),但它甚至会将=,*和-都视为字母。请您帮我一下。谢谢

尝试

    String s = "BC+D*E-=";

    for (int i = 0; i < s.length(); i++) {
        char charAt2 = s.charAt(i);
        if (Character.isLetter(charAt2)) {
            System.out.println(charAt2 + "is a alphabet");
        }
    }

使用包装器类 Character#isLetter(char)方法

Character.isLetter(char c);

并一次检查每个字母。

此代码段可能会对您有所帮助。

 String startingfrom = "BC+D*E-".toUpperCase();
        char chararray[] = startingfrom.toCharArray();
        for(int i = 0; i < chararray.length; i++) {
                            int value = (int)chararray[i];
                            if((value >= 65 && value <= 90) || (value >= 97 && value <= 122))
                                System.out.println(chararray[i]+ " is an alphabate");
                            else 
                                System.out.println(chararray[i]+ " is not an alphabate");
        }

将字符串分成一个数组并对其进行迭代。

String word = "BC+D*E-"
for (char c : word.toCharArray()) {
    if(!(Character.isLetter(c)) {
        System.out.println("Not a character!");
        break;
    }
}

您可以使用英文字母的ASCII值。 例如

for (char c : "BC+D*E-".toCharArray())
{
  int value = (int) c;
  if ((value >= 65 && value <= 90) || (value >= 97 && value <= 122))
  {
    System.out.println("letter");
  }
}

您可以在此处找到ASCII表: http : //www.asciitable.com/

String stringEx =  "BC+D*E-";
for (char string : stringEx.toCharArray()) {
    if ( ( (char)string > 64 ) && ((char)string < 91) )
        System.out.println("It is character");
    if ( ( (char)string > 96 ) && ((char)string < 123) )
    System.out.println("It is character");
}

您可以使用此代码。 这也可能对您有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM