📜  Java.lang。字符类 - 方法 |设置 2

📅  最后修改于: 2022-05-13 01:55:39.014000             🧑  作者: Mango

Java.lang。字符类 - 方法 |设置 2

lang.Character_methods

朗。字符方法


Java.lang。字符类 - 方法 |设置 1

此处讨论字符类的以下方法:

  1. toUpperCase() : Java.lang.toUpperCase(char arg)方法根据 Unicode 值将参数中的给定字符转换为其大写。
    句法 :
    public static char toUpperCase(char arg)
    Parameters : 
    arg : character to be converted to Upper case
    Return : 
    Upper case character of the argumented character.  
    
  2. toLowerCase() : Java.lang.toLowerCase(char arg)方法根据 Unicode 值将参数中的给定字符转换为其小写。
    句法 :
    public static char toLowerCase(char arg)
    Parameters : 
    arg : character to be converted to Lower case
    Return : 
    Lower case character of the argumented character.  
    
  3. isMirrored(): Java.lang。 字符.isMirrored(char arg)方法根据 Unicode 值检查传递的参数是否被镜像。在从右到左的文本中显示时,镜像字符应使其字形水平镜像。例如,'\u0028' 左括号在语义上被定义为左括号。这将在从左到右的文本中显示为“(”,但在从右到左的文本中显示为“)”。
    例如: [ ] { } ( )
    句法 :
    public static boolean isMirrored(char arg)
    Parameters : 
    arg : argumented character 
    true if char 'arg' is mirrored, else false.
    

    解释 toUpperCase()、toLowerCase()、isMirrored() 方法使用的Java代码

    // Java program explaining Character class methods
    // toUpperCase(), toLowerCase(), isMirrored()
    import java.lang.Character;
    public class NewClass
    {
        public static void main(String[] args)
        {
            // Use of toUpperCase() method
            Character g1 = new Character('g');
            Character g2 = new Character('O');
      
            char chUpper = Character.toUpperCase(g1);
            char chUpper1 = Character.toUpperCase(g2);
      
            System.out.println("Upper value for g : "+ chUpper);
            System.out.println("Upper value for O : "+ chUpper1);
            System.out.println("");        
              
            // Use of toLowerCase() method
            char chLower = Character.toLowerCase(g1);
            char chLower1 = Character.toLowerCase(g2);
            System.out.println("Lower value for G : "+ chLower);
            System.out.println("Lower value for O : "+ chLower1);
            System.out.println("");
      
            // Use of isMirrored() method
            Character g3 = new Character('-');
            Character g4 = new Character(')');
            Character g5 = new Character('{');
            Character g6 = new Character(']');
      
            boolean checkBool1 = Character.isMirrored(g3);
            boolean checkBool2 = Character.isMirrored(g4);
            boolean checkBool3 = Character.isMirrored(g5);
            boolean checkBool4 = Character.isMirrored(g6);
              
            System.out.println("Checking - : "+ checkBool1);
            System.out.println("Checking ) : "+ checkBool2);
            System.out.println("Checking { : "+ checkBool3);
            System.out.println("Checking ] : "+ checkBool4);
        }
    }
    

    输出:

    Upper value for g : G
    Upper value for O : O
    
    Lower value for G : g
    Lower value for O : o
    
    Checking - : false
    Checking ) : true
    Checking { : true
    Checking ] : true
    
  4. reverseBytes(): Java.lang。 字符 .reverseBytes()方法通过反转参数字符中的字节顺序来返回一个字符。
    句法 :
    public static char reverseBytes()
    Parameters : 
    cP : code point, need to check
    Return : 
    character with reversed order of bytes in the argumented character
    
  5. isAlphabetic(): Java.lang。 字符.isAlphabetic(int cP)方法检查参数字符(代码点)是否是字母表
    句法 :
    public static boolean isAlphabetic(int codePoint)
    Parameters : 
    cP : code point, need to check.  
    Return : 
    True if character is a Unicode alphabet, else false
    Exception : 
    --> NullPointerException
    --> IndexOutOfBoundsException 
    
  6. isValidCodePoint(): Java.lang。 字符.isValidCodePoint(int cP)方法检查参数 Unicode 是否实际有效
    句法 :
    public static boolean isValidCodePoint(int codePoint)
    Parameters : 
    cP : code point to be tested  
    Return : 
    true if Min code point <  Argumented code point < Max code point
    

    解释使用 isValidCodePoint()、reverseBytes()、isAlphabetical() 方法的Java代码

    // Java program explaining Character class methods
    // isValidCodePoint(), reverseBytes(), isAlphabetical()
    import java.lang.Character;
    public class NewClass
    {
        public static void main(String[] args)
        {
            // Use of reverseBytes() method
            Character g1 = new Character('1');
            Character g2 = new Character('s');
      
            char chreverse = Character.reverseBytes(g1);
            char chreverse1 = Character.reverseBytes(g2);
      
            System.out.println("Reverse char for 1 : "+ chreverse);
            System.out.println("Revrese char for s : "+ chreverse1);
            System.out.println("");
      
            // Use of isAlphabetical() method
            int c1 = 101, c2 = 132;
            boolean check1 = Character.isAlphabetic(c1);
            boolean check2 = Character.isAlphabetic(c2);
            System.out.println("Is Unicode 66 alphabetic  : "+ check1);
            System.out.println("Is Unicode 132 alphabetic : "+ check2);
            System.out.println("");
      
            // Use of isValidCodePoint() method
            int c3 = 0x012343, c4 = 0x01344ffff;
            boolean check4 = Character.isValidCodePoint(c4);
            boolean check3 = Character.isValidCodePoint(c3);
            System.out.println("Validity check : "+ check3);
            System.out.println("Validity check : "+ check4);
        }
    }
    

    输出:

    Reverse char for 1 : ?
    Reverse char for s : ?
    
    Is Unicode 66 alphabetic  : true
    Is Unicode 132 alphabetic : false
    
    Validity check : true
    Validity check : false