java从字符串中提取数字
随便给你⼀个含有数字的字符串,⽐如:
String s="eert343dfg56756dtry66fggg89dfgf";
那我们如何把其中的数字提取出来呢?⼤致有以下⼏种⽅法,正则表达式,集合类,还有就是String类提供的⽅法。
1 String类提供的⽅法:
package 测试练习;
import java.util.*;
public class get_StringNum {
/**
*2012.6.2
*/
public static void main(String[] args) {
String str = "love23next234csdn3423javaeye";
im();
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 正则表达式
import java.util.*;
import Matcher;
import Pattern;
public class get_StringNum {
/**
*2012.6.2
*/
public static void main(String[] args) {
String a="love23next234csdn3423javaeye";
String regEx="[^0-9]";
Pattern p = Patternpile(regEx);
Matcher m = p.matcher(a);
System.out.println( m.replaceAll("").trim());
}
}
output:
232343423
Pattern ,Matcher是软件包⾥的两个类,具体⽤法⼤家可以查阅⼀下api。同样也不能单个提取数字。
3 集合类库
import java.util.*;
import Matcher;
import Pattern;
public class get_StringNum {
/**
*2012.6.2
*/
public static void main(String[] args) {
String a="love23next234csdn3423javaeye";
List<String> digitList = new ArrayList<String>();
Pattern p = Patternpile("[^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]
相同的思路:
import java.util.*;
import Matcher;
import Pattern;
public class get_StringNum {
/**
*2012.6.2
*/
public static void main(String[] args) {
String a="love23next234csdn3423javaeye";
List<String> ss = new ArrayList<String>();
for(String placeAll("[^0-9]", ",").split(",")){
if (sss.length()>0)
ss.add(sss);
}
System.out.print(ss);
}
}
output:
[2, 3, 2, 3, 4, 3, 4, 2, 3]
很明显,利⽤正则表达式我们就可以分别提取数字了。另外还有⼀个利⽤查阅⽂档出的答案,如下:
/**
* 从字符串⽂本中获得数字
*@param
text
*@return
*/
publicstatic
List<Long>
getDigit(String text) {
List<Long>
digitList =new
ArrayList<Long>();
Pattern p=
Patternpile("(\\d+)");
Matcher m=
p.matcher(text);
while
(m.find()) {
String find=
digitList.add(Long.valueOf(find));
}return
digitList;
}
两个⽤正则表达式匹配的判断⽅法,如下;
Java代码
1. // 判断⼀个字符串是否都为数字
2. public boolean isDigit(String strNum) {
3. return strNum.matches("[0-9]{1,}");
4. }
5.
6. // 判断⼀个字符串是否都为数字
7. public boolean isDigit(String strNum) {
8.    Pattern pattern = Patternpile("[0-9]{1,}");
9.    Matcher matcher = pattern.matcher((CharSequence) strNum);
10. return matcher.matches();
11. }
12.
13. //截取数字
14. public String getNumbers(String content) {
15.        Pattern pattern = Patternpile("\\d+");
16.        Matcher matcher = pattern.matcher(content);
17. while (matcher.find()) {
字符串截取数字部分18. up(0);
19.        }
20. return"";
21.    }
22.
23. // 截取⾮数字
24. public String splitNotNumber(String content) {
25.    Pattern pattern = Patternpile("\\D+");
26.    Matcher matcher = pattern.matcher(content);
27. while (matcher.find()) {
28. up(0);
29.    }
30. return"";
31. }
⾄此,关于从字符串中提取数字的⽂章就写到这,欢迎⼤家提意见。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。