W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
Character類的一個對象包裝一個char值。
字符類包含isLetter()和isDigit()方法來檢查字符是否為字母和數(shù)字。
toUpperCase()和toLowerCase()方法將字符轉(zhuǎn)換為大寫和小寫。
該類提供了一個構(gòu)造函數(shù)和一個工廠valueOf()方法來從char創(chuàng)建對象。
charValue()方法返回對象包裝的char。
以下代碼顯示了如何創(chuàng)建Character對象以及如何使用它們的一些方法:
public class Main { public static void main(String[] args) { // Using the constructor Character c1 = new Character("A"); // Using the factory method - preferred Character c2 = Character.valueOf("2"); Character c3 = Character.valueOf("-"); // Getting the wrapped char values char cc1 = c1.charValue(); char cc2 = c2.charValue(); char cc3 = c3.charValue(); System.out.println("c1 = " + c1); System.out.println("c2 = " + c2); System.out.println("c3 = " + c3); // Using some Character class methods on c1 System.out.println("isLowerCase c1 = " + Character.isLowerCase(cc1)); System.out.println("isDigit c1 = " + Character.isDigit(cc1)); System.out.println("isLetter c1 = " + Character.isLetter(cc1)); System.out.println("Lowercase of c1 = " + Character.toLowerCase(cc1)); // Using some Character class methods on c2 System.out.println("isLowerCase c2 = " + Character.isLowerCase(cc2)); System.out.println("isDigit c2 = " + Character.isDigit(cc2)); System.out.println("isLetter c2 = " + Character.isLetter(cc2)); System.out.println("Lowercase of c2 = " + Character.toLowerCase(cc2)); System.out.println("Uppercase of c3 = " + Character.toUpperCase(cc3)); } }
上面的代碼生成以下結(jié)果。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: