java判断是否unicode_检查字符串在Java中是否仅包含unicode字母或数字

要检查字符串在Java中是否仅包含unicode字母或数字,我们使用带有决策语句的isLetterOrDigit()方法和charAt()方法。

isLetterOrDigit(char ch)方法确定特定字符(Unicode ch)是字母还是数字。它返回一个布尔值,为true或false。

声明-java.lang.Character.isLetter()方法声明如下-public static boolean isLetter(char ch)

该charAt()方法返回给定索引处的字符值。它属于Java中的String类。索引必须在0到length()-1之间。

声明-java.lang.String.charAt()方法声明如下-public char charAt(int index)

让我们看一下Java中的程序,以检查String是否仅包含Unicode字母或数字。

示例public class Example {

boolean check(String s) {

if (s == null) // checks if the String is null {

return false;

}

int len = s.length();

for (int i = 0; i 

//检查字符是否既不是字母也不是数字

//如果它既不是字母也不是数字,则将返回false-

if ((Character.isLetterOrDigit(s.charAt(i)) == false)) {

return false;

}

}

return true;

}

public static void main(String [] args) {

Example e = new Example();

String s = "10@4"; // returns false due to special character presence

String s1 = "13y4"; // returns true

String s2 = "1000"; // returns true

String s3= "abcd"; // returns true

System.out.println("String "+s+" has only unicode letters or digits : "+e.check(s));

System.out.println("String "+s1+" has only unicode letters or digits : "+e.check(s1));

System.out.println("String "+s2+" has only unicode letters or digits : "+e.check(s2));

System.out.println("String "+s3+" has only unicode letters or digits : "+e.check(s3));

}

}

输出结果String 10@4 has only unicode letters or digits : false

String 13y4 has only unicode letters or digits : true

String 1000 has only unicode letters or digits : true

String abcd has only unicode letters or digits : true

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值