java 如何在字符串中判断是否有数字

2025-04-01 10:08:46
推荐回答(4个)
回答1:

可以通过正则表达式进行判断

public static boolean isNumeric(String str){ 
  Pattern pattern = Pattern.compile("[0-9]*"); 
  return pattern.matcher(str).matches();  
}

解释说明:

static Pattern compile(String regex) 
将给定的正则表达式编译并赋予给Pattern类 

回答2:

   // 按指定模式在字符串查找
      String line = "This order was placed for QT2! OK?";
      String pattern = ".*\\d+.*";

      // 创建 Pattern 对象
      Pattern r = Pattern.compile(pattern);
      // 现在创建 matcher 对象
      Matcher m = r.matcher(line);
      System.err.println(m.matches());

回答3:

java 如何在字符串中判断是否有数字
下表列出了Turbo C中各类整型量所分配的内存字节数及数的表示范围。
类型说明符 数的范围 字节数
int -32768~32767 即-215~(215-1) 2
unsigned int 0~65535 即0~(216-1) 2
short int -32768~32767 即-215~(215-1) 2
unsigned short int 0~65535 即0~(216-1) 2
long int -2147483648~2147483647即-231~(231-1) 4
unsigned long 0~4294967295 即0~(232-1) 4
以13为例:
int型:
00 00 00 00 00 00 11 01
short int型:
00 00 00 00 00 00 11 01
long int型:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 11 01
unsigned int型:
00 00 00 00 00 00 11 01
unsigned short int型:
00 00 00 00 00 00 11 01

回答4:

循环判断。。。