黑马程序员技术交流社区

标题: 关于判断一个字符串是否含有非数字 [打印本页]

作者: 15242694137    时间: 2016-6-30 16:34
标题: 关于判断一个字符串是否含有非数字
//判断一个字符串是否都为数字  
02. public boolean isDigit(String strNum) {  
03.     return strNum.matches("[0-9]{1,}");  
04. }  
05.   
06. // 判断一个字符串是否都为数字  
07. public boolean isDigit(String strNum) {  
08.     Pattern pattern = Pattern.compile("[0-9]{1,}");  
09.     Matcher matcher = pattern.matcher((CharSequence) strNum);  
10.     return matcher.matches();  
11. }
12.
13. //截取数字  
14. public String getNumbers(String content) {  
15.     Pattern pattern = Pattern.compile("\\d+");  
16.     Matcher matcher = pattern.matcher(content);  
17.     while (matcher.find()) {  
18.        return matcher.group(0);  
19.     }  
20.     return "";  
21. }  
22.   
23. // 截取非数字  
24. public String splitNotNumber(String content) {  
25.     Pattern pattern = Pattern.compile("\\D+");  
26.     Matcher matcher = pattern.matcher(content);  
27.     while (matcher.find()) {  
28.         return matcher.group(0);  
29.     }  
30.     return "";  
31. }  
32.// 判断一个字符串是否含有数字
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;
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2