2014-07-25 28 views

回答

0

如果它總是在格式number words,那麼您可以:

int number = Integer.parseInt(lineString.replace(/\D+/, '')); // This uses a regex to remove all letters 

OR

int number = lineString.split(" ")[0]; // Splits string by spaces and gets the first token, which will be numbers 

其中lineString是要分析,像1 something的字符串。

0

int result = Integer.parseInt(mString.substring(0,1));

0

沒有正則表達式的一個簡單的方法:

private int readNum(String input){ 
    String buffer = ""; 
    char[] chars = input.toCharArray(); 
    for(char c: chars){ 
     if(Character.isDigit(c)) 
      buffer = buffer + c; 
     else 
      break; 
    } 
    return Integer.parseInt(buffer); 
} 
0

您可以使用NumberFormat類 try { Log.i("TAG",((Number)NumberFormat.getInstance().parse("123efdsf4")).intValue()+""); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } 的方法「解析」,直到找到一個字符,此方法將解析字符串。

相關問題