java - How to check if a string is a number เช็คว่าเป็นตัวเลขหรือตัวอักษร หรือไม่
Input
142Sp94r09y+j0i50*5i198++07m6743 |
จาก input ให้ดึงเอาเฉพาะตัวเลขมาแสดง.
Output
14294090505198076743 |
Sample Code
String intput = "142Sp94r09y+j0i50*5i198++07m6743";
String number ="";
for(int i = 0 ; i < intput.length() ;i++){
char c = intput.charAt(i);
if(Character.isDigit(c)){
number += c;
}
}
System.out.println("output : " +number);
ตัวอย่างการใช้งาน
เช่น เอาไปแก้ปัญหาในโจทย์ Opt String(http://goo.gl/YlfFyX)
เนื้อหาที่เกียวข้อง / ควรศึกษาเพิ่มเติมถ้าไม่เข้าใจ
- การใช้งาน if-else(http://goo.gl/3kFL66)
- การใช้งาร for-loop(http://goo.gl/qpNu1d)
Subscribe to:
Comments
(
Atom
)
No comments :
Post a Comment