• java 判断字符串中是否包含数字


     
    // 判断一个字符串是否都为数字
    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中判断字符串是否为数字的四种方法 :
    1.用JAVA自带的函数
    public static boolean isNumeric(String str) {
      for (int i = str.length(); --i >= 0;) {
        if (!Character.isDigit(str.charAt(i))) {
          return false;
         }
      }
      return true;
    }
     
    2.可以使用lang3下的包下StringUtils类的方法isNumeric() 
    String str="1235456";
    boolean flag =StringUtils.isNumeric(str);
    3..用正则表达式 
    public static boolean isNumeric(String str) {
      Pattern pattern = Pattern.compile("[0-9]*");
      return pattern.matcher(str).matches();
    }
    4.用ASCII码 
    public static boolean isNumeric(String str) {
      for (int i = str.length(); --i >= 0;) {
        int chr = str.charAt(i);
        if (chr < 48 || chr > 57)
        return false;
      }
      return true;
    }
     
  • 相关阅读:
    恼人的CON(转)
    CINRAD/SB 多普勒天气雷达
    如何将两个栅格数据图层求和并求并集
    javascript设置asp.net程序开始运行时ie最大化显示
    ArcGIS Engine中数据的加载(转载)
    发现了一个arcgis engine的一个bug
    带余除法
    第二数学归纳法
    concat和concat_ws()区别及MySQL的几个实用字符串函数
    oracle查询语句大全
  • 原文地址:https://www.cnblogs.com/lhn9527/p/14024122.html
Copyright © 2020-2023  润新知