Checking for Alphabets in a String in java

后端 未结 6 1450
灰色年华
灰色年华 2021-01-16 21:46

I have a string \"BC+D*E-\". I want to check each character of the string whether it is an alphabet or not. I tried using isLetter() but it does consider even the = , * and

相关标签:
6条回答
  • 2021-01-16 22:09

    Use the Wrapper class Character#isLetter(char) method

    Character.isLetter(char c);
    

    and check each letter one at a time.

    0 讨论(0)
  • 2021-01-16 22:11
    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");
    }
    

    You can use this code. This may also helpful to you.

    0 讨论(0)
  • 2021-01-16 22:15

    This snippet may help you.

     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");
            }
    
    0 讨论(0)
  • 2021-01-16 22:15

    You can use ASCII value of English letters. e.g.

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

    You can find ASCII table here: http://www.asciitable.com/

    0 讨论(0)
  • 2021-01-16 22:23

    Try

        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");
            }
        }
    
    0 讨论(0)
  • 2021-01-16 22:28

    Break the String into an array and iterate over it.

    String word = "BC+D*E-"
    for (char c : word.toCharArray()) {
        if(!(Character.isLetter(c)) {
            System.out.println("Not a character!");
            break;
        }
    }
    
    0 讨论(0)
提交回复
热议问题