2012-05-24 26 views

risposta

35
String str="sdfvsdf68fsdfsf8999fsdf09"; 
String numberOnly= str.replaceAll("[^0-9]", ""); 

aggiornamento:

String str="fgdfg12°59'50\" Nfr | gdfg: 80°15'25\" Efgd"; 
String[] spitStr= str.split("\\|"); 

String numberOne= spitStr[0].replaceAll("[^0-9]", ""); 
String numberSecond= spitStr[1].replaceAll("[^0-9]", ""); 
+0

Se usato questo sto ricevendo solo alfabeti, ma io Ho bisogno solo di valori numerici –

+0

Ho aggiornato vedere di nuovo la mia risposta. –

+0

L'ho capito, grazie .. –

4
public static String getOnlyNumerics(String str) { 

    if (str == null) { 
     return null; 
    } 

    StringBuffer strBuff = new StringBuffer(); 
    char c; 

    for (int i = 0; i < str.length() ; i++) { 
     c = str.charAt(i); 

     if (Character.isDigit(c)) { 
      strBuff.append(c); 
     } 
    } 
    return strBuff.toString(); 
} 
+0

grazie ha funzionato per me! – xbadal

1
public static int extractNumberFromAnyAlphaNumeric(String alphaNumeric) { 
    alphaNumeric = alphaNumeric.length() > 0 ? alphaNumeric.replaceAll("\\D+", "") : ""; 
    int num = alphaNumeric.length() > 0 ? Integer.parseInt(alphaNumeric) : 0; // or -1 
    return num; 
} 

È possibile impostare il valore a 0 o -1 (cosa fare se nessun numero si trova nella alfanumerico a tutti) secondo la vostra needs

Problemi correlati