Java 判断字符是否为数字

发布时间:2024-12-15 01:52

数字编码,将数字转化为有意义的符号 #生活技巧# #学习技巧# #高效记忆法#

最新推荐文章于 2023-10-31 14:38:57 发布

willproud 于 2013-12-18 11:53:12 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

String str = "123abc";

for(int i =0, length = str.length(); i<length;i++)

{

System.out.println("char: "+str.charAt(i));

System.out.println("ASCII: "+(int)str.charAt(i));

if (Character.isDigit(str.charAt(i)))

System.out.println("Character.isDigit(str.charAt(0)) - yes");

else

System.out.println("Character.isDigit(str.charAt(0)) - no");

if(str.charAt(i) > 48 && str.charAt(i) <57)

System.out.println("str.charAt(0) > 48 && str.charAt(0) <57 - yes");

else

System.out.println("str.charAt(0) > 48 && str.charAt(0) <57 - no");

if((int)str.charAt(i) > 48 && (int)str.charAt(i) <57)

System.out.println("(int)str.charAt(0) > 48 && (int)str.charAt(0) <57 - yes");

else

System.out.println("(int)str.charAt(0) > 48 && (int)str.charAt(0) <57 - no");

if(str.charAt(i) <= '9' && str.charAt(i) >= '0')

System.out.println("str.charAt(0) <= '9' && str.charAt(0) >= '0' - yes");

else

System.out.println("str.charAt(0) <= '9' && str.charAt(0) >= '0' - no");

}

输出结果:

char: 1

ASCII: 49

Character.isDigit(str.charAt(0)) - yes

str.charAt(0) > 48 && str.charAt(0) <57 - yes

(int)str.charAt(0) > 48 && (int)str.charAt(0) <57 - yes

str.charAt(0) <= '9' && str.charAt(0) >= '0' - yes

char: 2

ASCII: 50

Character.isDigit(str.charAt(0)) - yes

str.charAt(0) > 48 && str.charAt(0) <57 - yes

(int)str.charAt(0) > 48 && (int)str.charAt(0) <57 - yes

str.charAt(0) <= '9' && str.charAt(0) >= '0' - yes

char: 3

ASCII: 51

Character.isDigit(str.charAt(0)) - yes

str.charAt(0) > 48 && str.charAt(0) <57 - yes

(int)str.charAt(0) > 48 && (int)str.charAt(0) <57 - yes

str.charAt(0) <= '9' && str.charAt(0) >= '0' - yes

char: a

ASCII: 97

Character.isDigit(str.charAt(0)) - no

str.charAt(0) > 48 && str.charAt(0) <57 - no

(int)str.charAt(0) > 48 && (int)str.charAt(0) <57 - no

str.charAt(0) <= '9' && str.charAt(0) >= '0' - no

char: b

ASCII: 98

Character.isDigit(str.charAt(0)) - no

str.charAt(0) > 48 && str.charAt(0) <57 - no

(int)str.charAt(0) > 48 && (int)str.charAt(0) <57 - no

str.charAt(0) <= '9' && str.charAt(0) >= '0' - no

char: c

ASCII: 99

Character.isDigit(str.charAt(0)) - no

str.charAt(0) > 48 && str.charAt(0) <57 - no

(int)str.charAt(0) > 48 && (int)str.charAt(0) <57 - no

str.charAt(0) <= '9' && str.charAt(0) >= '0' - no

网址:Java 判断字符是否为数字 https://www.yuejiaxmz.com/news/view/477236

相关内容

python 判断字符串是否相等 ==,is, in 误区。
javascript判断数字大小
【Java数据结构】字符串常量池
java中空串 “”!=null..字符串要用equals判等
如何判断一个数是否为2的N次方
判断两个Integer是否相等,使用==会产生的问题分析
如何判断办公家具是否是绿色环保的?
JAVA题目
Java解决if(!=null)臭名昭著的判空处理(Optional)
String.format()字符串格式化

随便看看