我有包含6个整数的随机字符串 例如:
如何只将字符串中的整数拆分为只有
我需要使用string.split();
我试过了:
int [] test = new int [7];
string.split (test);
但它没有给出任何输出。我哪里错了?
答案 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+", ""));
}