java 判断一个字符串中的数字:是否为数字、是否包含数字、截取数字
提取字符串中数字并排序输出实例demo,为了方便就直接用中文命名了,别介意哦!
import java.util.Arrays; import java.util.regex.Matcher; import java.util.regex.Pattern; import pingshilianxi.main; public class 提取数字并排序 { public static void main(String[] args) { String a="好ABC13网7GMNQQ2049PN5F"; //正则表达式匹配指定范围数字 String regex="[^0-9]"; //pattern是一个正则表达式经编译后的表现模式 Pattern p=Pattern.compile(regex); //一个matcher对象是一个状态机器,它根据pattern对象作为匹配模式对一个字符串展开匹配调查 Matcher m=p.matcher(a); //将输入的字符串中非数字部分用空格取代并存入一个字符串 String str=m.replaceAll("").trim(); //以空格为分隔符在将数字存入一个String数组中 String[] shuzu=str.split(""); Arrays.sort(shuzu); for(String b:shuzu){ System.out.println(b); } } }
题外话:
JavaScript中判断一个字符是否为数字,用函数:isDigit();
一、判断一个字符串是否都为数字
package com.cmc.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DigitUtil {
public static void main(String[] args) {
String str="123d";
System.out.println(isDigit2(str));
}
//判断一个字符是否都为数字
public static boolean isDigit(String strNum){
return strNum.matches("[0-9]{1,}");
}
// 判断一个字符串是否都为数字
public static boolean isDigit2(String strNum) {
Pattern pattern = Pattern.compile("[0-9]{1,}");
Matcher matcher = pattern.matcher((CharSequence) strNum);
return matcher.matches();
}
}
二、判断字符串中是否包含数字
// 判断一个字符串是否含有数字
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;
}
三、截取字符串中的数字
package com.cmc.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DigitUtil {
public static void main(String[] args) {
String str="123dsd99";
System.out.println(getNumbers(str)); //===> 123
System.out.println(splitNotNumber(str)); //===> dsd
}
//截取数字 【读取字符串中第一个连续的字符串,不包含后面不连续的数字】
public static String getNumbers(String content) {
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
return matcher.group(0);
}
return "";
}
// 截取非数字
public static String splitNotNumber(String content) {
Pattern pattern = Pattern.compile("\\D+");
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
return matcher.group(0);
}
return "";
}
}
四、判断字符串是否为数字的方法
(一)用java自带函数
public static void main(String[] args) {
String str="12399";
System.out.println(isNumeric(str));
}
//判断一个字符串是否为数字
public static boolean isNumeric(String str){
for (int i = str.length(); --i>=0;) {
if(!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
(二)用正则表达式
public static void main(String[] args) {
String str="123p99";
System.out.println(isNumeric2(str));
}
public static boolean isNumeric2(String str){
Pattern pattern=Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}
(三)用ASCII码
public static void main(String[] args) {
String str="12399";
System.out.println(isNumeric3(str));
}
//判断一个字符串是否为数字
public static boolean isNumeric3(String str){
for(int i=str.length(); --i>=0;){
char c=str.charAt(i);
if (c < 48 || c > 57)
return false;
}
return true;
}
JAVA修炼塔,技术世界的探知与交流,欢迎你的加入-----群号:535296702


浙公网安备 33010602011771号