首页 > 代码库 > java 字符串中是否有数字

java 字符串中是否有数字

http://www.cnblogs.com/zhangj95/p/4198822.html

http://www.cnblogs.com/sunzn/archive/2013/07/12/3186518.html

参考:

 1 package cn.sunzn.demo;
 2 
 3 import java.util.regex.Matcher;
 4 import java.util.regex.Pattern;
 5 
 6 public class Demo {
 7     public static void main(String[] args) {
 8         System.out.println(isContainChinese("中国China"));
 9     }
10 
11     public static boolean isContainChinese(String str) {
12 
13         Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
14         Matcher m = p.matcher(str);
15         if (m.find()) {
16             return true;
17         }
18         return false;
19     }
20 }

在自己的例子里面,find起作用

 1 package sdf;
 2 
 3 import java.util.regex.Matcher;
 4 import java.util.regex.Pattern;
 5 
 6 public class d {
 7 
 8     public static boolean isNumeric(String str) {
 9         for (int i = str.length(); --i >= 0;) {
10             if (!Character.isDigit(str.charAt(i))) {
11                 return false;
12             }
13         }
14         return true;
15     }
16     public static void main(String[] args) {
17         // TODO Auto-generated method stub
18 
19         String s1="25";
20         String s2="补考";
21         Pattern pattern = Pattern.compile("\\d+");
22         
23           Matcher matcher1 = pattern.matcher(s1);
24           //boolean b1= matcher1.matches();
25           boolean b1= matcher1.find();
26           System.out.println(b1);
27           
28           Matcher matcher2 = pattern.matcher(s2);
29           //boolean b2= matcher2.matches();
30           boolean b2= matcher2.find();
31         System.out.println(b2);
32         
33         //System.out.println(isNumeric(s1));
34        //System.out.println(isNumeric(s2));
35 
36     }
37 
38 }

 

java 字符串中是否有数字