Using regular expressions we can remove any non-digits from a string as shown next
String text = "12 34 56"; String clean = text.replaceAll("[^\\d]", "" );
The regular expression
[^\d] (not that this is regular expression and you need to escape the backslash when used in Java) matches with anything that is not a digit. The same expression can be used to split a given string.