java从字符串中提取数字

随便给你一个含有数字的字符串,比如:

String s="eert343dfg56756dtry66fggg89dfgf";

那我们如何把其中的数字提取出来呢?大致有以下几种方法,正则表达式,集合类,还有就是String类提供的方法。

1 String类提供的方法:

[html]  view plain copy
  1. package 测试练习;  
  2. import java.util.*;  
  3. public class get_StringNum {  
  4.   
  5.   
  6. /**  
  7.  *2012.6.2  
  8.  */  
  9.   
  10. public static void main(String[] args) {  
  11. String str = "love23next234csdn3423javaeye";  
  12. str=str.trim();  
  13. String str2="";  
  14. if(str != null && !"".equals(str)){  
  15. for(int i=0;i<str.length();i++){  
  16. if(str.charAt(i)>=48 && str.charAt(i)<=57){  
  17. str2+=str.charAt(i);  
  18. }  
  19. }  
  20.   
  21. }  
  22. System.out.println(str2);  
  23. }  
  24.   
  25. }  
  26.   
  27. output:  
  28.   
  29. 232343423  
  30.   
  31. 这个方法有个明显的缺点,只能把数字全部提取到一起,不能分别提取。当然也可以改进,有兴趣的朋友可以试试。  
  32.   
  33. 2 正则表达式  
  34.   
  35. import java.util.*;  
  36. import java.util.regex.Matcher;  
  37. import java.util.regex.Pattern;  
  38. public class get_StringNum {  
  39.   
  40.   
  41. /**  
  42.  *2012.6.2  
  43.  */  
  44.   
  45. public static void main(String[] args) {  
  46. String a="love23next234csdn3423javaeye";  
  47. String regEx="[^0-9]";     
  48. Pattern p = Pattern.compile(regEx);     
  49. Matcher m = p.matcher(a);     
  50. System.out.println( m.replaceAll("").trim());  
  51. }  
  52.   
  53. }  
  54.   
  55. output:  
  56.   
  57. 232343423  
  58.   
  59. Pattern ,Matcher是java.util.regex软件包里的两个类,具体用法大家可以查阅一下api。同样也不能单个提取数字。  
  60.   
  61. 3 集合类库  
  62.   
  63. import java.util.*;  
  64. import java.util.regex.Matcher;  
  65. import java.util.regex.Pattern;  
  66. public class get_StringNum {  
  67.   
  68.   
  69. /**  
  70.  *2012.6.2  
  71.  */  
  72.   
  73. public static void main(String[] args) {  
  74.    String a="love23next234csdn3423javaeye";  
  75. List<String> digitList = new ArrayList<String>();  
  76. Pattern p = Pattern.compile("[^0-9]");  
  77. Matcher m = p.matcher(a);  
  78. String result = m.replaceAll("");  
  79. for (int i = 0; i < result.length(); i++) {  
  80. digitList.add(result.substring(i, i+1));  
  81.   
  82. }  
  83. System.out.println(digitList);  
  84.   
  85. }  
  86.   
  87. }  
  88.   
  89. output:  
  90.   
  91. [2, 3, 2, 3, 4, 3, 4, 2, 3]  
  92.   
  93. 相同的思路:  
  94.   
  95. import java.util.*;  
  96. import java.util.regex.Matcher;  
  97. import java.util.regex.Pattern;  
  98. public class get_StringNum {  
  99.   
  100.   
  101. /**  
  102.  *2012.6.2  
  103.  */  
  104.   
  105. public static void main(String[] args) {  
  106.                String a="love23next234csdn3423javaeye";  
  107.         List<String> ss = new ArrayList<String>();  
  108.         for(String sss:s.replaceAll("[^0-9]", ",").split(",")){  
  109.             if (sss.length()>0)  
  110.                 ss.add(sss);  
  111.         }  
  112.         System.out.print(ss);  
  113.   
  114.   
  115. }  
  116.   
  117. }  
  118.   
  119. output:  
  120.   
  121. [2, 3, 2, 3, 4, 3, 4, 2, 3]  
  122. 很明显,利用正则表达式我们就可以分别提取数字了。  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值