Java: removing the non-numbers characters from a string.

This is one of those code warrior snippets with two-lines that one never seems to find around the Internet, so I'm posting here for reference.

If you have a String and want to remove all the characters inside the string that are not numbers, this works:

public static int justNumbers(String input){
         String temp = input.replaceAll("[^0-9]""");
         return Integer.parseInt(temp);
     }

 Seems pretty simple doesn't it? Now look around the Internet and see the other techniques being proposed.

Hope this helps you.

No comments:

Post a Comment