java从字符串中提取数字

Wesley13
• 阅读 635

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

}

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Karen110 Karen110
3年前
一篇文章带你了解JavaScript日期
日期对象允许您使用日期(年、月、日、小时、分钟、秒和毫秒)。一、JavaScript的日期格式一个JavaScript日期可以写为一个字符串:ThuFeb02201909:59:51GMT0800(中国标准时间)或者是一个数字:1486000791164写数字的日期,指定的毫秒数自1970年1月1日00:00:00到现在。1\.显示日期使用
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
3个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Wesley13 Wesley13
3年前
Java日期时间API系列31
  时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总毫秒数,是所有时间的基础,其他时间可以通过时间戳转换得到。Java中本来已经有相关获取时间戳的方法,Java8后增加新的类Instant等专用于处理时间戳问题。 1获取时间戳的方法和性能对比1.1获取时间戳方法Java8以前
Stella981 Stella981
3年前
HIVE 时间操作函数
日期函数UNIX时间戳转日期函数: from\_unixtime语法:   from\_unixtime(bigint unixtime\, string format\)返回值: string说明: 转化UNIX时间戳(从19700101 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式举例:hive   selec
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Wesley13 Wesley13
3年前
unity将 -u4E00 这种 编码 转汉字 方法
 unity中直接使用 JsonMapper.ToJson(对象),取到的字符串,里面汉字可能是\\u4E00类似这种其实也不用转,服务器会通过类似fastjson发序列化的方式,将json转对象,获取对象的值就是中文但是有时服务器要求将传参中字符串中类似\\u4E00这种转汉字,就需要下面 publ
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
9个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这