当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Character isLetter()用法及代码示例


Character 类的 isLetter(char ch) 方法确定给定(或指定)的字符是否为字母。

如果 Character.getType(ch) 提供的一般类别类型是以下之一,则将字符视为字母:

  • UPPERCASE_LETTER。
  • LOWERCASE_LETTER。
  • TITLECASE_LETTER。
  • MODIFIER_LETTER。
  • OTHER_LETTER

用法

public static boolean isLetter(char ch)

参数

ch: 这是需要测试的字符。

返回值

isLetter(char ch) 方法返回一个布尔值,即如果给定(或指定)字符是字母,则返回 true。否则,该方法返回 false。

例子1

public class JavaCharacterisLetterExample1 {
    public static void main(String[] args) {
    // Create three char primitives ch1, ch2 and ch3.
      char ch1, ch2, ch3;
      // Assign the values to ch1, ch2 and ch3.
      ch1 = 'A';
      ch2 = '9';
      ch3 = 'e';
      // Create three boolean primitives b1, b2 and b3;
      boolean b1, b2, b3;
      // Check whether ch1, ch2 and ch3 are letters or not and assign the results to b1, b2 and b3.
      b1 = Character.isLetter(ch1);
      b2 = Character.isLetter(ch2);
      b3 = Character.isLetter(ch3);

      String str1 = "The character "+ch1 + " is a letter:" + b1;
      String str2 = "The character "+ch2 + " is a letter:" + b2;
      String str3 = "The character "+ch3 + " is a letter:" + b3;

      // Print the values of b1, b2 and b3.
      System.out.println( str1 );
      System.out.println( str2 );
       System.out.println( str3 );
   }
}

输出:

The character A is a letter:true
The character 9 is a letter:false
The character e is a letter:true

例子2

public class JavaCharacterisLetterExample2 {
    public static void main(String[] args) {
    // Create three char primitives ch1, ch2 and ch3.
      char ch1, ch2, ch3;
      // Assign the values to ch1, ch2 and ch3.
      ch1 = '1';
      ch2 = '*';
      ch3 = 'e';
      // Create three boolean primitives b1, b2 and b3;
      boolean b1, b2, b3;
      // Check whether if ch1, ch2 and ch3 are letters or not and assign the result to b1, b2 and b3.
      b1 = Character.isLetter(ch1);
      b2 = Character.isLetter(ch2);
      b3 = Character.isLetter(ch3);

      String str1 = "The character "+ch1 + " is a letter:" + b1;
      String str2 = "The character "+ch2 + " is a letter:" + b2;
      String str3 = "The character "+ch3 + " is a letter:" + b3;

      // Print the values of b1, b2 and b3.
      System.out.println( str1 );
      System.out.println( str2 );
       System.out.println( str3 );
   }
}

输出:

The character 1 is a letter:false
The character * is a letter:false
The character e is a letter:true

例子3

public class JavaCharacterisLetterExample3 {
    public static void main(String[] args) {
    // Create three char primitives ch1, ch2 and ch3.
      char ch1, ch2, ch3;
      // Assign the values to ch1, ch2 and ch3.
      ch1 = '1';
      ch2 = ')';
      ch3 = '*';
      // Create three boolean primitives b1, b2 and b3;
      boolean b1, b2, b3;
      // Check whether  ch1, ch2 and ch3 are letters or not and assign the result to b1, b2 and b3.
      b1 = Character.isLetter(ch1);
      b2 = Character.isLetter(ch2);
      b3 = Character.isLetter(ch3);

      String str1 = "The character "+ch1 + " is a letter:" + b1;
      String str2 = "The character "+ch2 + " is a letter:" + b2;
      String str3 = "The character "+ch3 + " is a letter:" + b3;

      // Print the values of b1, b2 and b3.
      System.out.println( str1 );
      System.out.println( str2 );
       System.out.println( str3 );
   }
}

输出:

The character 1 is a letter:false
The character ) is a letter:false
The character * is a letter:false

Java 字符 isLetter(int codePoint) 方法

字符类的 isLetter(int codePoint) 方法确定给定(或指定)字符是否为字母。

如果 Character.getType(codePoint) 提供的一般类别类型是以下之一,则将字符视为字母:

  • UPPERCASE_LETTER。
  • LOWERCASE_LETTER。
  • TITLECASE_LETTER。
  • MODIFIER_LETTER。
  • OTHER_LETTER

用法

public static boolean isLetter(int codePoint)

参数

上述方法只需要一个参数:

a.)codePoint 是需要测试的字符。

返回值

如果给定(或指定)字符是字母,则 isLetter(int codePoint) 方法返回一个布尔值,即 true。否则,该方法返回 false。

示例 4

public class JavaCharacterisLetterExample4 {
    public static void main(String[] args) {
    // Create four char primitives ch1, ch2, ch3 and ch4.
     int codePoint1;
     int codePoint2;
     int codePoint3;
     int codePoint4;
     
      // Assign the values to ch1, ch2, ch3 and ch4.
      codePoint1 = 56;
      codePoint2 = 110;
      codePoint3= 123;
      codePoint4 = 315;
      // Create four boolean primitives b1, b2, b3 and b4;
      boolean b1, b2, b3, and b4;
      // Check whether  ch1, ch2, ch3 and ch4 are letters or not and assign the result to b1, b2, b3 and b4.
      b1 = Character.isLetter(codePoint1);
      b2 = Character.isLetter(codePoint2);
      b3 = Character.isLetter(codePoint3);
      b4 = Character.isLetter(codePoint4);

      String str1 = "The first codePoint  "+codePoint1 + " is a letter:" + b1;
      String str2 = "The second codePoint "+codePoint2 + " is a letter:" + b2;
      String str3 = "The third codePoint  "+codePoint3 + " is a letter:" + b3;
      String str4 = "The fourth codePoint "+codePoint4 + " is a letter:" + b3;

      // Print the values of b1, b2, b3 and b4.
      System.out.println(str1);
      System.out.println(str2);
      System.out.println(str3);
      System.out.println(str4);
   }
}

输出:

The first codePoint  56 is a letter:false
The second codePoint 110 is a letter:true
The third codePoint  123 is a letter:false
The fourth codePoint 315 is a letter:false

例 5

import java.util.Scanner;
public class JavaCharacterisLetterExample5 {
    public static void main(String[] args) {
    // Ask for user input
       System.out.print("Enter the first input:");
    // Use the Scanner class to get the user input
       Scanner s1 = new Scanner(System.in);
    // Gets the user input
       char[] value1 = s1.nextLine().toCharArray();
    // Check whether the user input is a letter or not.
       for (char ch1:value1) {
   boolean result1 = Character.isLetter(ch1);
    // print the result
       if(result1){
System.out.println("The character '" + ch1 + "' is a letter. ");				
                  }
     else{
System.out.println("The character '" + ch1 + "' is not a letter.");
           }		
       System.out.print("Enter the second input:");
       Scanner s2 = new Scanner(System.in);
       char[] value2 = s2.nextLine().toCharArray();
       for (char ch2:value2) {
      boolean result2 = Character.isLetter(ch2);
      if(result2){
System.out.println("The character '" + ch2 + "' is a letter. ");				
                }
      else{
System.out.println("The character '" + ch2 + "' is not a letter.");
        }		
      System.out.print("Enter the third input:");
       Scanner s3 = new Scanner(System.in);
       char[] value3 = s3.nextLine().toCharArray();
       for (char ch3:value3) {
      boolean result3 = Character.isLetter(ch3);
      if(result2){
System.out.println("The character '" + ch3 + "' is a letter. ");				
                }
      else{
System.out.println("The character '" + ch3 + "' is not a letter.");
           }		
         }
       }
     }
  }
}

输出:

Enter the first input:8
The character '8' is not a letter.
Enter the second input:T
The character 'T' is a letter. 
Enter the third input:&
The character '&' is a letter. 




相关用法


注:本文由纯净天空筛选整理自 Java Character isLetter() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。