ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java从字符串中提取数字

2021-09-17 17:33:23  阅读:164  来源: 互联网

标签:regex 提取 String matcher util Pattern 字符串 java public


string类函数的补充说明:

trim()方法返回调用字符串对象的一个副本,但是所有起始和结尾的空格都被删除了,例子如下:String s="    Hello World      ".trim();就是把"Hello World"放入s中。(注意使用时必须赋值)

 

1 String类提供的方法:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package 测试练习; import Java.util.*; public class get_StringNum {     /**  *2016.10.25  */   public static void main(String[] args) { String str = "love23next234csdn3423javaeye"; str=str.trim(); String str2=""; if(str != null && !"".equals(str)){ for(int i=0;i<str.length();i++){ if(str.charAt(i)>=48 && str.charAt(i)<=57){ str2+=str.charAt(i); } }   } System.out.println(str2); }   }   output:   232343423

这个方法有个明显的缺点,只能把数字全部提取到一起,不能分别提取。当然也可以改进,有兴趣的朋友可以试试。

2 正则表达式

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class get_StringNum {     /**  *2016.10.25  */   public static void main(String[] args) { String a="love23next234csdn3423javaeye"; String regEx="[^0-9]" Pattern p = Pattern.compile(regEx);  Matcher m = p.matcher(a);  System.out.println( m.replaceAll("").trim()); }   }   output:   232343423

Pattern ,Matcher是java.util.regex软件包里的两个类,具体用法大家可以查阅一下api。同样也不能单个提取数字。

  • Pattern类的作用在于编译正则表达式后创建一个匹配模式.
  • Matcher类使用Pattern实例提供的模式信息对正则表达式进行匹配
  • Pattern complie(String regex) 
    由于Pattern的构造函数是私有的,不可以直接创建,所以通过静态方法compile(String regex)方法来创建,将给定的正则表达式编译并赋予给Pattern类

  • String pattern() 返回正则表达式的字符串形式,其实就是返回Pattern.complile(String regex)的regex参数


String regex = "\\?|\\*";
Pattern pattern = Pattern.compile(regex);
String patternStr = pattern.pattern();//返回\?\*

replaceAll() 方法使用给定的参数 replacement 替换字符串所有匹配给定的正则表达式的子字符串

3 集合类库

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class get_StringNum {     /**  *2016.10.25  */   public static void main(String[] args) {   String a="love23next234csdn3423javaeye"; List<String> digitList = new ArrayList<String>(); Pattern p = Pattern.compile("[^0-9]"); Matcher m = p.matcher(a); String result = m.replaceAll(""); for (int i = 0; i < result.length(); i++) { digitList.add(result.substring(i, i+1));   } System.out.println(digitList);   }   }   output:   [2, 3, 2, 3, 4, 3, 4, 2, 3]

相同的思路:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class get_StringNum {     /**  *2016.10.25  */   public static void main(String[] args) {         String a="love23next234csdn3423javaeye";     List<String> ss = new ArrayList<String>();     for(String sss:s.replaceAll("[^0-9]", ",").split(",")){       if (sss.length()>0)         ss.add(sss);     }     System.out.print(ss);     }   }   output:   [2, 3, 2, 3, 4, 3, 4, 2, 3]

很明显,利用正则表达式我们就可以分别提取数字了。

另外还有一个利用查阅文档找出的答案,如下:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 /**  * 从字符串文本中获得数字   *@param  text   *@return      */      publicstatic  List<Long>  getDigit(String text) {   List<Long>  digitList =new  ArrayList<Long>();      Pattern p=  Pattern.compile("(\\d+)");      Matcher m=  p.matcher(text);   while  (m.find()) {   String find=  m.group(1).toString();   digitList.add(Long.valueOf(find));   }return  digitList;   }

两个用正则表达式匹配的判断方法,如下;

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 // 判断一个字符串是否都为数字 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 ""; }

标签:regex,提取,String,matcher,util,Pattern,字符串,java,public
来源: https://www.cnblogs.com/hongyi66/p/15305564.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有