「java是否包含数字」java判断字符串只包含数字字母

博主:adminadmin 2022-11-25 17:12:07 47

今天给各位分享java是否包含数字的知识,其中也会对java判断字符串只包含数字字母进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java 怎么判断一个字符串中是否包含数字

java中判断字符串是否为数字的方法:

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.compile("[0-9]*");

Matcher isNum = pattern.matcher(str);

if( !isNum.matches() ){

return false;

}

return true;

}

3.使用org.apache.commons.lang

org.apache.commons.lang.StringUtils;

boolean isNunicodeDigits=StringUtils.isNumeric("aaa123456789");

下面的解释:

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 如何高效判断一个数组是否包含另一个数字

判断数组是否包含另一个数字,肯定要先遍历数组。 而遍历数组中,for循环效率很高。

int x = 3;//测试数字

int[] a = new int[]{1,2,3,4};

int[] b = new int[]{3,4,5};

boolean flag = false;//标示 是否包含,false为不包含,true为包含

for(int i=0;ia.length;i++){

for(int j=0;jb.length;j++){

if(a[i] == x b[j] == x){

flag = true;

}

}

}

if(flag){

System.out.println("数组a和数组b 都包含数字:"+x);

}else{

System.out.println("数组a或数组b 不包含数字:"+x);

}

java中如何用正则表达式判断一个字符串中是否包含0-9的数字?

// 判断一个字符串是否都为数字

public boolean isDigit(String strNum) {

return strNum.matches("[0-9]{1,}");

}

// 判断一个字符串是否都为数字

public boolean isDigit(String strNum) {

Pattern pattern = Pattern.compile("[0-9]{1,}");

Matcher matcher = pattern.matcher((CharSequence) strNum);

return matcher.matches();

}

//截取数字

public String getNumbers(String content) {

Pattern pattern = Pattern.compile("\\d+");

Matcher matcher = pattern.matcher(content);

while (matcher.find()) {

return matcher.group(0);

}

return "";

}

// 截取非数字

public String splitNotNumber(String content) {

Pattern pattern = Pattern.compile("\\D+");

Matcher matcher = pattern.matcher(content);

while (matcher.find()) {

return matcher.group(0);

}

return "";

}

// 判断一个字符串是否含有数字

public boolean hasDigit(String content) {

boolean flag = false;

Pattern p = Pattern.compile(".*\\d+.*");

Matcher m = p.matcher(content);

if (m.matches())

flag = true;

return flag;

}

java判断一个数组是否含有该数字?

可以把数组转成list,利用list的contains方法来判断

Integer[] arr = new Integer[] { 1, 2, 3, 4 };

ListInteger list = Arrays.asList(arr);

if (list.contains(5)) {

System.out.println("包含");

} else {

System.out.println("不包含");

}

public class Test {

public static void main(String[] args) {

boolean flag = false;

int[] is = new int[1000];

for (int i = 0; i  is.length; i++) {

is[i] = i;

}

Scanner scanner = new Scanner(System.in);

System.out.println("请输入一个整数");

int num = scanner.nextInt();

for (int i : is) {

if (num == i) {

flag = true;

}

}

if (flag) {

System.out.println("有这个整数");

} else {

System.out.println("没有这个整数");

}

}

}

java判断一个字符串里是否有数字:

如果只是判断,可与用Integer.parseInt(String)如果是数字,就没有异常,如果有异常,就不是数字或者用正则表达式 return string.matches("\\d+\\.?\\d*")); 这个语句就是用来判断的 \\d+表示一个或者多个数字\\.? 表示一个或这没有小数点 \\d * 表示0个或者多个数字

java是否包含数字的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java判断字符串只包含数字字母、java是否包含数字的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-25,除非注明,否则均为首码项目网原创文章,转载请注明出处。