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

String s="eert343dfg56756dtry66fggg89dfgf";

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

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。同样也不能单个提取数字。

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 "";
}

以上

/**提取中文、数字、英文*/

@Test

public void test (){
    String str = "……^1dsf  の  adS   DFASFSADF阿德斯防守对方asdfsadf37《?:?@%#¥%#¥%@#$%#@$%^><?1234";  
    String regEx="[a-zA-Z0-9\\u4e00-\\u9fa5]";  
        Pattern   p   =   Pattern.compile(regEx);     
        Matcher   m   =   p.matcher(str);
        StringBuffer sb = new StringBuffer();
        while(m.find()){
        sb.append(m.group());
        }
        System.out.println(sb);

}

1 提取中文:regEx=“[\\u4e00-\\u9fa5]";

2 提取数字:regEx=“[0-9]";

3 提取英文:regEx=“[a-zA-Z0-9]";

4 提取英文和数字:regEx=“[a-zA-Z0-9]";

java从字符串中提取数字的简单实例的更多相关文章

  1. java从字符串中提取数字

    string类函数的补充说明: trim()方法返回调用字符串对象的一个副本,但是所有起始和结尾的空格都被删除了,例子如下:String s=" Hello World ".tri ...

  2. java截取字符串中的数字

    java从字符串中提取数字 随便给你一个含有数字的字符串,比如: String s="eert343dfg56756dtry66fggg89dfgf"; 那我们如何把其中的数字提取 ...

  3. Oracle中如何判断字符串是否全为数字,以及从任意字符串中提取数字

    本文介绍了判断字符串是否全为数字的4种办法,另外还介绍了一个translate函数的小技巧,从任意字符串中提取数字(调用2次translate函数).这个办法是一个公司同事发现的,用起来很方便,但理解 ...

  4. 在sqlserver中如何从字符串中提取数字,英文,中文,过滤重复字符

    原文:在sqlserver中如何从字符串中提取数字,英文,中文,过滤重复字符 最近做项目的时候,经常用到一个字符串中的某些字符,处理起来非常的棘手,现在用下面的方法就可以很方便的从字符串中处理你想要的 ...

  5. JAVA从字符串中提取纯数字

    /** * 从字符串中提取纯数字 * @param str * @return */ public static String getNumeric(String str) { String regE ...

  6. Java去除字符串中 除数字和逗号以外的符号

    例: public static void main(String[] args) { // 去除字符串中 除数字和逗号以外的符号 String str = "_1066,_1068,_10 ...

  7. Js 字符串中提取数字

    一 parseInt()方法: 首先想到的是js提供的parseInt方法,例子: var str ="4500元"; var num = parseInt(str); alert ...

  8. SQL函数:字符串中提取数字,英文,中文,过滤重复字符(转)

    --提取数字 IF OBJECT_ID('DBO.GET_NUMBER2') IS NOT NULL DROP FUNCTION DBO.GET_NUMBER2 GO )) ) AS BEGIN BE ...

  9. mysql—从字符串中提取数字(类型1)

    select reason,CHAR_LENGTH(reason),mid(reason,5,CHAR_LENGTH(reason)-5)+0 from `table` 解释: CHAR_LENGTH ...

随机推荐

  1. 06-码蚁JavaWeb之Servlet生命周期与基本配置

    学习地址:[撩课-JavaWeb系列1之基础语法-前端基础][撩课-JavaWeb系列2之XML][撩课-JavaWeb系列3之MySQL][撩课-JavaWeb系列4之JDBC][撩课-JavaWe ...

  2. Spring Boot 打包jar部署服务器

    部署方式:打包成jar部署 部署方式有两种,一种是传统的war包,另一种是打包成jar,推荐第二种方式部署 部署准备 1. jar包内置tomcat,无需服务器安装tomcat环境 2.需要JDK,且 ...

  3. tensorflow(一):图片处理

    一.图片处理 1.图片存取 tf.gfile import tensorflow as tf import matplotlib.pyplot as plt image_bytes = tf.gfil ...

  4. 如何使Wpf浏览器应用程序被完全信任运行

    原文地址链接:http://blogs.microsoft.co.il/maxim/2008/03/05/how-to-run-wpf-xbap-as-full-trust-application/ ...

  5. Code Signal_练习题_arrayChange

    You are given an array of integers. On each move you are allowed to increase exactly one of its elem ...

  6. 关于log4j知识

    今天下午接触到log4j知识,花了几个小时,百度了一圈,总算是懂了一些. log4j的作用:log4j是一个日志输出的插件专门用来进行日志管理的,根据我的理解就是用来执行我们用来检测程序bug的Sys ...

  7. WinForm实现Rabbitmq官网6个案例-Topics

    代码: namespace RabbitMQDemo { public partial class Topics : Form { private string exchangeName = &quo ...

  8. LintCode2016年8月22日算法比赛----将数组重新排序以构造最小值

    将数组重新排序以构造最小值 题目描述 给定一个整数数组,请将其重新排序,以构造最小值. 样例 给定[3,32,321],通过将数组重新排序,可构造6个可能性的数字: 3+32+321=332321 3 ...

  9. maven配置以及在eclipse Mars2中的使用

    通过1.maven的作用 maven也是一种项目管理工具,主要能够将项目中的jar包移除,通过索引的方式调动本地或者远程仓库中的包,从而实现项目的构建.另一个作用能够应用于大型项目的分模块开发. 2. ...

  10. Android沉浸式状态栏

    private void initWindows() { Window window = getWindow(); int color = getResources().getColor(androi ...