不規則的字符串中截取數字



import java.util.Arrays;


public class TrimString {

public static int[] getNumber(String str){

//定義字符串中有3個數字,也可以更多

int num[] = new int[3];
String value="";
for(int i=0,j=0;i<str.length();i++){

if(str.charAt(i)<='9'&&str.charAt(i)>='0'){
value+=str.charAt(i);
}else if(!(str.charAt(i)<='9'&&str.charAt(i)>='0')){
if(!value.equals("")){
num[j] = Integer.parseInt(value);
j++;
value="";
}
}
}
return num;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(getNumber("[1 , 10,\" 10\"]")));
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章