java/javascript从字符串中提取数字的简单实例

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

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

}

 

1、第一种方法:

用字符串的方法,遍历字符串每一个字符,当字符的编码介于0-9之间时将字符存在变量tmp中,否则变量追加到数组并且清空。

<script>
var str = 'haj123sdk54hask33dkhalsd879';

function findNum(str){
    
    var arr = [];
    var tmp = '';
    
    for(var i=0;i<str.length;i++){
        if( str.charAt(i)<='9' && str.charAt(i)>='0' ){
            tmp += str.charAt(i);
        }
        else{
            if(tmp){
                arr.push(tmp);
                tmp = '';
            }
        }
    }
    
  //在循环外加一个判断,是因为当字符串遍历到最后一位9的时候,不会走else里面的内容,无法加到数组
    if(tmp){
        arr.push(tmp);
        tmp = '';
    }
    
    return arr;
    
}

alert( findNum(str) );   //[123,54,33,879]

</script>

 

2、第二种方法:正则的match匹配。

function findNum(str){
    return str.match(/\d+/g);
}

alert( findNum(str) );   //[123,54,33,879]

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值