您现在的位置是:首页 >

怎么判断一个字是不是形声字 Java 中怎样判断一个字串全是数字

火烧 2023-04-15 18:10:27 1050
Java 中怎样判断一个字串全是数字 Java 中怎样判断一个字串全是数字Java中判断字串是否全是数字:可以使用正则表示式: u lic oolea i Numeric Stri g tr { Pa
怎么判断一个字是不是形声字 Java 中怎样判断一个字串全是数字

Java 中怎样判断一个字串全是数字  

Java 中怎样判断一个字串全是数字

Java中判断字串是否全是数字:

可以使用正则表示式:

public boolean isNumeric(String str) { Pattern pattern = Pattern.pile("[0-9]*"); Matcher isNum = pattern.matcher(str); if (!isNum.matches()) { return false; } return true; }

但是这个方法并不安全,没有对字串进行空校验。 
在程式执行的时候很容易丢掷异常。 
例如执行:

public static void main(String[] args) { String str = null; System.out.println(BarcodeChecksum.INSTANCE.isNumeric(str)); }

就会丢掷异常:

Exception in thread "main" java.lang.NullPointerException at java.util.regex.Matcher.getTextLength(Matcher.java:1140) at java.util.regex.Matcher.reset(Matcher.java:291) at java.util.regex.Matcher.<init>(Matcher.java:211) at java.util.regex.Pattern.matcher(Pattern.java:888) at .ossez.bcu.util.BarcodeChecksum.isNumeric(BarcodeChecksum.java:37) at .ossez.bcu.util.BarcodeChecksum.main(BarcodeChecksum.java:53)

所以这个方法并不准确。 
如果执行:

public static void main(String[] args) { String str = ""; System.out.println(BarcodeChecksum.INSTANCE.isNumeric(str)); }

将会返回 true。 
这说明这个方法没有对空字串进行校验。 
可以使用 Apache 的 StringUtils.isNumeric() 函式进行判断。 
这个函式位于 .apache.mons.lang.StringUtils;  中。 
但是,需要注意,如果传入引数为 "" 同样也会你存在判断不准确的情况,这时候需要首先对需要进行判断的引数进行非空校验,然后删除传入资料中的空格。

public static void main(String[] args) { String str = ""; System.out.println(StringUtils.isNumeric(str)); }

上面这个函式将会返回 true。 
请先 trim 资料

java中怎样判断一个字串全是数字?

1.使用Character.isDigit(char)判断
char num[] = str.toCharArray();把字串转换为字元阵列
StringBuffer title = new StringBuffer();使用StringBuffer类,把非数字放到title中
StringBuffer hire = new StringBuffer();把数字放到hire中
for (int i = 0; i < num.length; i++) {
判断输入的数字是否为数字还是字元
if (Character.isDigit(num[i])) {把字串转换为字元,再呼叫Character.isDigit(char)方法判断是否是数字,是返回True,否则False
hire.append(num[i]); 如果输入的是数字,把它赋给hire} else {title.append(num[i]); 如果输入的是字元,把它赋给title}}}
2.使用型别转换判断try {String str="123abc";
int num=Integer.valueOf(str);把字串强制转换为数字
return true;如果是数字,返回True
} catch (Exception e) {
return false;如果丢掷异常,返回False}
3.使用正则表示式判断
String str = "";
boolean isNum = str.matches("[0-9]+");
+表示1个或多个(如"3"或"225"),*表示0个或多个([0-9]*)(如""或"1"或"22"),?表示0个或1个([0-9]?)(如""或"7")
ps:这个方法只能用于判断是否是正整数

Java中怎样判断一个字串是否是数字?

ava中判断字串是否为数字的方法:
1.用JAVA自带的函式
public static boolean isNumeric(String str){
for (int i = 0; i < str.length(); i++){
System.out.println(str.charAt(i));
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
2.用正则表示式
首先要import java.util.regex.Pattern 和 java.util.regex.Matcher
public boolean isNumeric(String str){
Pattern pattern = Pattern.pile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ){
return false;
}
return true;
}
3.使用.apache.mons.lang
.apache.mons.lang.StringUtils;
boolean isNunicodeDigits=StringUtils.isNumeric("aaa123456789");
:jakarta.apache./mons/lang/api-release/index.下面的解释:
isNumeric
public static boolean isNumeric(String str)Checks if the String contains only unicode digits. A decimal point is not a unicode digit and returns false.
null will return false. An empty String ("") will return true.
StringUtils.isNumeric(null) = false
StringUtils.isNumeric("") = true
StringUtils.isNumeric(" ") = false
StringUtils.isNumeric("123") = true
StringUtils.isNumeric("12 3") = false
StringUtils.isNumeric("ab2c") = false
StringUtils.isNumeric("12-3") = false
StringUtils.isNumeric("12.3") = false
Parameters:
str - the String to check, may be null
Returns:
true if only contains digits, and is non-null
上面三种方式中,第二种方式比较灵活。
第一、三种方式只能校验不含负号“-”的数字,即输入一个负数-199,输出结果将是false;
而第二方式则可以通过修改正则表示式实现校验负数,将正则表示式修改为“^-?[0-9]+”即可,修改为“-?[0-9]+.?[0-9]+”即可匹配所有数字。

java 怎么判断一个字串全是同一个字元

String s = "Replace the string with yourself.";根据要求,字串有2个相同的字元,即判定为有相同字元public boolean haveSameChar(String s) { for (int i=0;i=0) { return true; } } return false;}

  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码