1 String 类提供的方法:
?
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
- if(str.charAt(i)>=48 && str.charAt(i)<=57){
- str2+=str.charAt(i);
- }
- }
- }
- System.out.println(str2);
- }
- }
- output:
- 232343423
这个方法有个明显的缺点, 只能把数字全部提取到一起, 不能分别提取当然也可以改进, 有兴趣的朋友可以试试
2 正则表达式
- ?
- 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 同样也不能单个提取数字
3 集合类库
- ?
- 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 digitList = new ArrayList();
- 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]
相同的思路:
- ?
- 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 ss = new ArrayList();
- 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]
很明显, 利用正则表达式我们就可以分别提取数字了
另外还有一个利用查阅文档找出的答案, 如下:
- ?
- /**
- * 从字符串文本中获得数字
- *@param
- text
- *@return
- */
- publicstatic
- List
- getDigit(String text) {
- List
- digitList =new
- ArrayList();
- 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;
- }
两个用正则表达式匹配的判断方法, 如下;
- ?
- // 判断一个字符串是否都为数字
- 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 "";
- }
来源: http://www.bubuko.com/infodetail-2522650.html