2010-04-04 12 views
7

Mam coś takiego jak "ali123hgj". Chcę mieć 123 w liczbie całkowitej. jak mogę zrobić to w java?numer z ciągu znaków w java

+4

Co z '' abc123def567ghi "' lub '" abcdef "'? – kennytm

+1

Masz zawsze 3 znaki przed numerem lub jest to tylko przykład? – lbedogni

+0

to nie jest tylko trzy znaki .it to liczba od 0 lub więcej znaków. może to być "123", "sdfs", "123fdhf", "fgdkjhgf123" –

Odpowiedz

8

Poniższa RegExp (patrz http://java.sun.com/docs/books/tutorial/essential/regex/):

\d+ 

przez:

final Pattern pattern = Pattern.compile("\\d+"); // the regex 
final Matcher matcher = pattern.matcher("ali123hgj"); // your string 

final ArrayList<Integer> ints = new ArrayList<Integer>(); // results 

while (matcher.find()) { // for each match 
    ints.add(Integer.parseInt(matcher.group())); // convert to int 
} 
11
int i = Integer.parseInt("blah123yeah4yeah".replaceAll("\\D", "")); 
// i == 1234 

Uwaga jak to będzie "scalić" cyfr z różnych części strun razem pod jednym numerem . Jeśli mimo to masz tylko jeden numer, to nadal działa. Jeśli chcesz tylko pierwszy numer, a następnie można zrobić coś takiego:

int i = Integer.parseInt("x-42x100x".replaceAll("^\\D*?(-?\\d+).*$", "$1")); 
// i == -42 

regex jest nieco bardziej skomplikowana, ale to w zasadzie zastępuje cały łańcuch z pierwszej sekwencji cyfr, który zawiera ona (z opcjonalnym minus), przed użyciem Integer.parseInt do parsowania na liczbę całkowitą.

0

Można chyba zrobić wzdłuż tych linii:

Pattern pattern = Pattern.compile("[^0-9]*([0-9]*)[^0-9]*"); 
Matcher matcher = pattern.matcher("ali123hgj"); 
boolean matchFound = matcher.find(); 
if (matchFound) { 
    System.out.println(Integer.parseInt(matcher.group(0))); 
} 

To łatwo dostosować do wielu grup numerów, jak również. Kod ma charakter orientacyjny: nie został przetestowany.

0
int index = -1; 
for (int i = 0; i < str.length(); i++) { 
    if (Character.isDigit(str.charAt(i)) { 
     index = i; // found a digit 
     break; 
    } 
} 
if (index >= 0) { 
    int value = String.parseInt(str.substring(index)); // parseInt ignores anything after the number 
} else { 
    // doesn't contain int... 
} 
0
public static final List<Integer> scanIntegers2(final String source) { 
    final ArrayList<Integer> result = new ArrayList<Integer>(); 
    // in real life define this as a static member of the class. 
    // defining integers -123, 12 etc as matches. 
    final Pattern integerPattern = Pattern.compile("(\\-?\\d+)"); 
    final Matcher matched = integerPattern.matcher(source); 
    while (matched.find()) { 
    result.add(Integer.valueOf(matched.group())); 
    } 
    return result; 

wejścia "asg123d ddhd-2222-33sds --- --- --- 33dd 222 SS 234" zmieniają się tym ouput [123 -2222, -33, -222, - 33, 234]

1

To jest droga Google Guava #CharMatcher.

String alphanumeric = "12ABC34def"; 

String digits = CharMatcher.JAVA_DIGIT.retainFrom(alphanumeric); // 1234 

String letters = CharMatcher.JAVA_LETTER.retainFrom(alphanumeric); // ABCdef 

Jeśli tylko dbać, aby dopasować cyfry ASCII, użyj

String digits = CharMatcher.inRange('0', '9').retainFrom(alphanumeric); // 1234 

Jeśli tylko dbać, aby dopasować litery alfabetu łacińskiego, użyj

String letters = CharMatcher.inRange('a', 'z') 
         .or(inRange('A', 'Z')).retainFrom(alphanumeric); // ABCdef 
Powiązane problemy