java.lang.Character类方法| 1

作者: Arvin Chen 分类: Java 来源: Break易站(www.breakyizhan.com)

java.lang.Character类方法| 1

java.lang.Character类将原始数据类型的值 - char包装为数据类型为char的对象,并且此对象包含具有数据类型 - char的单个字段。这门课没有提供。有关字符操作的方法,如将它们从小写转换为大写。字符类基于Unicode标准提供字符信息。

public final class Character
   extends Object
      implements Serializable, Comparable

以下讨论Character类的以下方法:

  1. charCount():java.lang.charCount()方法使用Unicode 指针返回char值的数量来表示参数char值。Unicode代码点在U + 0000和U + 10FFFF之间和属于UTF-16编码的代码单元的16位char值的范围内用于字符的值。
    句法 :

    public static int charCount(int argchar)
     参数:
    argchar:char即它是统计的Unicode点
    返回: 
    2如果字符有效,即>或=到0X1000(补充字符); 其他1   
    
  2. charValue():java.lang.charValue()方法返回定义的char Object的原始字符值。
    句法 :

    public char charValue()
     返回:
    定义的char对象的原始字符值。
    
  3. codePointAt():java.lang.Character.codePointAt(char [] array,int position)方法返回存在于参数化位置的字符数组的Unicode Point。
    句法 :

    public static int codePointAt(char[] array, int position)
     参数:
    array:字符数组
    position:您需要的Unicode点值的字符的数组索引。  
    返回: 
    位于给定位置的字符数组的Unicode点  
    

解释charCount(),charValue(),codePointat()方法使用的Java代码

// Java program explaining Character class methods
// charCount(), charValue(), codePointat()
import java.lang.Character;
public class NewClass
{
    public static void main(String[] args)
    {
        // Use of charCount() method
        int geek = 0x9999,    // < 0x10000             geek1 = 0x10000,      // = 0x10000             geek2 = 0x10001;      // > 0x10000

        int check = Character.charCount(geek);
        int check1 = Character.charCount(geek1);
        int check2 = Character.charCount(geek2);

        if (check ==2)    // Checking for geek
            System.out.println("Valid Character geek");
        else
            System.out.println("Invalid Character geek");

        if (check1 ==2)   // Checking for geek1
            System.out.println("Valid Character geek1");
        else
            System.out.println("Invalid Character geek1");

        if (check2 ==2)   // Checking for geek2
            System.out.println("Valid Character geek2");
        else
            System.out.println("Invalid Character geek2");

        System.out.println("");


        // Use of charValue() method
        Character m;      // Character object m

        m = new Character('g');  // Assigning value g to m;

        char gfg;
        gfg = m.charValue();
        System.out.println("Primitive value of gfg : " +gfg);
        System.out.println("");


        // Use of codePointAt()
        char[] arg = new char[] { 'g', 'e', 'e', 'k', 's' };
        int val, val1, position  = 3;

        val = Character.codePointAt(arg, position);
        val1 = Character.codePointAt(arg, 0);

        System.out.println( "Unicode code point at " + position
                                                   + " : "+val );
        System.out.println( "Unicode code point at 0 : " + val1);
    }
}

输出:

Invalid Character geek
Valid Character geek1
Valid Character geek2

Primitive value of gfg : g

Unicode code point at 3 : 107
Unicode code point at 0 : 103
  1. codePointBefore():java.lang.Character.codePointBefore(char [] array,int position)方法返回提交位置前存在的字符数组的Unicode Point。
    句法 :

    public static int codePointBefore(char[] array, int position)
                              or
    public static int codePointBefore(char[] array, int position, int start)
    array:字符数组
    position:跟随您需要的Unicode点值的字符的数组索引。  
    start:字符数组的开始索引
    返回: 
    在给定位置之前存在的字符数组的Unicode点  
    
  2. codePointCount():java.lang.Character.codePointCount()方法返回no。子字符数组的Unicode Point。
    句法 :

    public static int codePointCount(char [] array,int start,int len)
     参数:
    array:字符数组
    start:数组的开始索引
    length:字符子数组的长度   
    返回: 
    no。子字符数组的Unicode Point。
    异常: 
    - > NullPointerException
    - > IndexOutOfBoundsException 
    
  3. compareTo():java.lang.Character.compareTo(Character argChar)方法将给定的字符与有争议的字符进行比较。
    句法 :

    public int compareTo(Character argChar)
     参数:
    argChar:与之相比的性格  
    返回: 
    = 0:如果两个字符相等 
    > 0:如果给出这个字符更大
    <0:如果争论角色更大
    

说明使用codePointBefore(),codePointCount(),compareTo()方法的Java代码

// Java program explaining Character class methods
// codePointBefore(), codePointCount(), compareTo()
import java.lang.Character;
public class NewClass
{
    public static void main(String[] args)
    {
        // Use of codePointBefore()
        char[] arg = new char[] { 'g', 'e', 'e', 'k', 's' };
        int position  = 4;

        int val = Character.codePointBefore(arg, position);
        int val1 = Character.codePointBefore(arg, 1);
        int val2 = Character.codePointBefore(arg, 3, 1);

        System.out.println( "Unicode code point before " + position +
                                                       " : " + val );
        System.out.println( "Unicode code point before 1 : " + val1 );
        System.out.println( "Unicode code point before 3 to 1 : "
                                                              + val2);
        System.out.println("");

        // Use of codePointCount()
        int count = Character.codePointCount(arg, 1,3 );

        System.out.println("No. of Unicode points : " + count);
        System.out.println("");

        // Use of compareTo()
        Character g1 = new Character('g');
        Character g2 = new Character('o');

        int check = g1.compareTo(g2);
        System.out.println("g1 < g2 : " + check);         int check1 = g2.compareTo(g1);         System.out.println("g2 > g1 : " + check1);
        int check2 = g2.compareTo(g2);
        System.out.println("g2 = g2 : " + check2);
    }
}

输出:

Unicode code point before 4 : 107
Unicode code point before 1 : 103
Unicode code point before 3 to 1 : 101

No. of Unicode points : 3

g1  g1 : 8
g2 = g2 : 0
  1. equals():java.lang.Character.equals()方法将当前的char对象与有争议的char对象进行比较。
    句法 :

    public boolean equals(Object charObj)
     参数:
    charObj:字符对象comapre 
    返回: 
    如果两个对象相等,则为true,否则为false。
    
  2. getNumericValue():java.lang.Character.getNumericValue(char arg)方法返回特定Unicode字符的int值。
    A - Z值范围u0041至u005A
    a -z值范围u0061至u007A
    语法:

    public static int getNumericValue(char arg)
     参数:
    arg:char值
    返回: 
    特定Unicode字符的int值。
    如果Unicode值不存在则返回-1。
    
  3. getType():java.lang.Character.getType(char arg)方法标识字符的一般类型
    A - Z值范围u0041至u005A
    a -z值范围u0061至u007A
    语法:

    public static int getType(char arg)
     参数:
    arg:char值
    返回: 
    用于表示其一般类型类别的有争议字符的int值。 
    

说明使用equals(),getNumericValue(),getType()方法的Java代码

// Java program explaining Character class methods
// equals(), getNumericValue(), getType()
import java.lang.Character;
public class NewClass
{
    public static void main(String[] args)
    {
        // Use of equals() method
        Character g1 = new Character('g');
        Character g2 = new Character('O');

        boolean check = g1.equals(g2);
        boolean check1 = g1.equals(g1);
        System.out.println("Are g and o equal? : " + check);
        System.out.println("Are g and g equal? : " + check1);
        System.out.println("");

        // Use of getNumericValue() method
        int c = Character.getNumericValue(g1);
        int c1 = Character.getNumericValue(g2);
        System.out.println("Int value for g : " + c);
        System.out.println("Int value for A : " + c1);
        System.out.println("");

        // Use of getType() method
        Character g3 = new Character('$');
        Character g4 = new Character('6');

        int r1 = Character.getType(g1);
        int r2 = Character.getType(g2);
        int r3 = Character.getType(g3);
        int r4 = Character.getType(g4);

        System.out.println("Type for lowercase : " + r1);
        System.out.println("Type for uppercase : " + r2);
        System.out.println("Type for currency  : " + r3);
        System.out.println("Type for numeric   : " + r4);
    }
}

输出:

Are g and o equal? : false
Are g and g equal? : true

Int value for g : 16
Int value for A : 24

Type for lowercase : 2
Type for uppercase : 1
Type for currency  : 26
Type for numeric   : 9
  •   本文标题:java.lang.Character类方法| 1 - Break易站
    转载请保留页面地址:https://www.breakyizhan.com/java/4419.html

    发表笔记

    电子邮件地址不会被公开。 必填项已用*标注

    更多阅读