-2

I Have string like so: "He3llo5"

How do I strip this String so that only the numbers remain?

Desired result:

35
Spectric
  • 30,714
  • 6
  • 20
  • 43
Nelson
  • 13
  • 1
  • Not a duplicate. The [alleged duplicate](https://stackoverflow.com/q/10372862/642706) involves a decimal separator. This question is simpler, integers, no decimal separator involved. This Question here probably *is* a duplicate, but not of that particular Question. – Basil Bourque May 04 '21 at 00:36

3 Answers3

1

Simplest solution: use a regex and .replaceAll():

String string="He3llo5";
String result = string.replaceAll("[^\\d.]", "");
System.out.println(result);

See this code execute at Ideone.com.

35

Alternatively, you could write your own method which utilizes Character.isDigit() and a StringBuilder like so:

public static String stripAlphabet(String s) {
    int len = s.length();
    StringBuilder str = new StringBuilder();
    for(int i = 0; i < len; i++) {
        char c = s.charAt(i);
        str.append(Character.isDigit(c) ? c : "");
    }
    return str.toString();
}

The following also produces 35 as the result:

String string="He3llo5";
String result = stripAlphabet(string);
System.out.println(result);
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Spectric
  • 30,714
  • 6
  • 20
  • 43
-1

You can use the Pattern Example

    Pattern intsOnly=Pattern.compile("\\d+");
        Matcher make=intsOnly.matcher(string);
        make.find();
        string=make.group();
Mohsen El Sayed
  • 135
  • 3
  • 11
  • 1
    I don't think this is correct. In the OP's example, it will give you `"3"` not `"35"` as required. – Stephen C May 04 '21 at 02:02
  • The **code in the Answer fails**, as [commented by Stephen C](https://stackoverflow.com/questions/67376943/strip-non-numerical-characters-from-string/67376973#comment119093300_67376971). See the [code execute at Ideone.com](https://ideone.com/U9vzH6), producing `3` rather than `35`. – Basil Bourque Jul 29 '22 at 16:32
-1

tl;dr

Integer.parseInt(
        "He3ll5"
                .codePoints()
                .filter( Character :: isDigit )
                .collect(
                        StringBuilder :: new ,
                        StringBuilder :: appendCodePoint ,
                        StringBuilder :: append
                )
                .toString()
)

See this code run live at Ideone.com.

35

Loop code points

Loop through each character’s Unicode code point number, asking if that code point represents a character that is a digit. If so, collect that code point or its conversion to a String.

Calling String#codePoints gives us an IntStream, a stream of the code point integer numbers.

We filter out the letters by calling Character#IsDigit. We collect the remaining digit characters into a StringBuilder.

From the string of digits, we parse as an int by calling Integer.parseInt.

String input = "He3llo5";

IntStream codePoints = input.codePoints();
String digits =
        codePoints
                .filter( Character :: isDigit )
                .collect( StringBuilder :: new ,
                        StringBuilder :: appendCodePoint ,
                        StringBuilder :: append )
                .toString();
int result = Integer.parseInt( digits );

result = 35

Avoid char

Be aware that the char type in Java is obsolete. That type fails to represent even half of the characters defined in Unicode. Use code point integers instead of char.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154