如何仅从字符串中拆分数字

时间:2014-06-03 17:25:11

标签: java regex string split

我有包含6个整数的随机字符串 例如:

  • 002500bla
  • 025478blu
  • 255699bli
  • 658896blo

如何只将字符串中的整数拆分为只有

  • BLA
  • BLI
  • BLO

我需要使用string.split();

来做到这一点

我试过了:

int [] test = new int [7];

string.split (test);

但它没有给出任何输出。我哪里错了?

2 个答案:

答案 0 :(得分:2)

您可以同时使用字符串的方法replaceAll()split

String s  = "002500bla 025478blu 255699bli 658896blo";
String[] parts = s.replaceAll("[0-9]", "").split("\\s+");
System.out.println(Arrays.toString(parts)); //=> [bla, blu, bli, blo]

答案 1 :(得分:1)

我希望这对你有所帮助。

public static void main(String[] args) {

   String str = "002500bla 025478blu 255699bli 658896blo";
   if (str == null || str.length() == 0) {
        System.out.println("null str"); 
   }

    //replace all digits
    System.out.println(str.replaceAll("[0-9]+/*\\.*[0-9]*",""));
    //replace all non-digits
    System.out.println(str.replaceAll("\\D+", ""));

   }