Java - Character getType() 方法



描述

Java 的 Character getType() 方法用于获取指示字符通用类别的值。众所周知,每个代码点在 Unicode 中都分配有一个通用类别值。

例如,所有大写字母都属于 UPPERCASE 类别;类似地,所有小写字母都属于 LOWERCASE 类别。符号具有各种子类别,例如 CURRENCY_SYMBOLS、MATH_SYMBOL 等。

但是,此方法的一种多态形式(采用 char 值作为参数)不适用于补充字符。要获取补充字符的通用类别的值,我们可以使用采用 codePoint 作为参数的多态形式。

语法

以下是 Java Character getType() 方法的语法

public static int getType(char ch)
(or)
public static int getType(int codePoint)

参数

  • ch − 要测试的字符

  • codePoint − 要测试的代码点

返回值

此方法返回一个表示字符通用类别的值。其返回类型为 int。

字符类型获取示例

以下示例演示了 Java Character getType(char ch) 方法的用法。在此程序中,我们创建了两个 char 变量并为它们分配了两个值。然后使用 getType() 方法,检索两个 char 的类型并打印结果。

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 character primitives ch1, ch2
      char ch1, ch2;

      // assign values to ch1, ch2
      ch1 = 'M';
      ch2 = '$';

      // create 2 int primitives i1, i2
      int i1, i2;

      // assign getType values of ch1, ch2 to i1, i2
      i1 = Character.getType(ch1);
      i2 = Character.getType(ch2);

      /**
       *  value 1 represents UPPERCASE_LETTER
       *  value 26 represents CURRENCY_SYMBOL
      */

      String str1 = "Category of " + ch1 + " is " + i1;
      String str2 = "Category of " + ch2 + " is " + i2;

      // print i1, i2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

Category of M is 1 
Category of $ is 26

代码点类型获取示例

以下示例演示了 Java Character getType(int codePoint) 方法的用法。在此程序中,我们创建了两个 int 变量并为它们分配了两个代码点。然后使用 getType() 方法,检索两个代码点的类型并打印结果。

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {
      int cp1, cp2;
      cp1 = 0x0034;
      cp2 = 0x006f;
      int i1, i2;
      i1 = Character.getType(cp1);
      i2 = Character.getType(cp2);

      /**
       *  value 9 represents DECIMAL_DIGIT_NUMBER
       *  value 2 represents LOWERCASE_LETTER
      */
      String str1 = "Category of cp1 is " + i1;
      String str2 = "Category of cp2 is " + i2;
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

Category of cp1 is 9
Category of cp2 is 2

通用类别字符类型获取示例

在下面的另一个示例中,我们将显示 Unicode 系统中基于大小写的字母的通用类别。该方法对大写字母返回 1,对小写字母返回 2。

package com.tutorialspoint;

public class getTypeDemo {
   public static void main(String[] args) {
      char ch1, ch2;
      ch1 = 'a';
      ch2 = 'A';
      int i1, i2;
      i1 = Character.getType(ch1);
      i2 = Character.getType(ch2);
      System.out.println("Category of " + ch1 + " is " + i1);
      System.out.println("Category of " + ch2 + " is " + i2);
   }
}

输出

如果我们编译并运行上面的程序,则输出将显示为:

Category of a is 2
Category of A is 1

破折号字符类型获取示例

符号中也有子类别。标点符号因其用法而异。让我们使用此方法查看两种不同类型的破折号符号的类别。

package com.tutorialspoint;

public class getTypeDemo {
   public static void main(String[] args) {
      char ch1, ch2;
      ch1 = '-';
      ch2 = '_';
      System.out.println("Category of " + ch1 + " is " + Character.getType(ch1));
      System.out.println("Category of " + ch2 + " is " + Character.getType(ch2));
   }
}

输出

如果我们编译代码并运行它,则将获得以下输出:

Category of - is 20
Category of _ is 23
java_lang_character.htm
广告
© . All rights reserved.