java eliminate numbers from string

http‮:s‬//www.lautturi.com
java eliminate numbers from string

To eliminate numbers from a string in Java, you can use the replaceAll() method of the String class and pass it a regular expression that matches any digit.

Here's an example of how to eliminate numbers from a string in Java:

public class NumberEliminator {
    public static void main(String[] args) {
        String input = "Hello 12345 World!";
        String output = input.replaceAll("\\d", "");  // Output: Hello  World!

        System.out.println(output);
    }
}

In this example, the replaceAll() method is used to replace all digits in the input string with an empty string. The regular expression \\d matches any digit, and the replaceAll() method replaces all occurrences of the regular expression with an empty string.

You can use a similar approach to eliminate other types of characters from a string, such as punctuation or whitespace. Just modify the regular expression to match the characters you want to eliminate.

Created Time:2017-11-03 15:57:12  Author:lautturi