ตัวละคร class เป็นคลาสย่อยของ Object คลาสและห่อค่าของถ่านประเภทดั้งเดิมในวัตถุ วัตถุประเภท Character มี ช่องเดียว ซึ่งมีประเภทเป็นถ่าน เราสามารถตรวจสอบว่าอักขระที่กำหนดในสตริงเป็นตัวเลข/ตัวอักษรหรือไม่โดยใช้ isDigit() วิธีการของ ตัวละคร ระดับ. isDigit() วิธีเป็นวิธีการคงที่และกำหนดว่าอักขระที่ระบุเป็น หลัก .
ตัวอย่าง
public class CharacterIsNumberOrDigitTest {
public static void main(String[] args) {
String str = "Tutorials123";
for(int i=0; i < str.length(); i++) {
Boolean flag = Character.isDigit(str.charAt(i));
if(flag) {
System.out.println("'"+ str.charAt(i)+"' is a number");
}
else {
System.out.println("'"+ str.charAt(i)+"' is a letter");
}
}
}
} ผลลัพธ์
'T' is a letter 'u' is a letter 't' is a letter 'o' is a letter 'r' is a letter 'i' is a letter 'a' is a letter 'l' is a letter 's' is a letter '1' is a number '2' is a number '3' is a number