InterviewSolution
Saved Bookmarks
| 1. |
Get all digits from a string in Java |
|
Answer» To DISPLAY the digits from a STRING, use Regular Expression. The string (with digits): 12345exampleTo get the digits use the \\D flag and the replaceAll(): replaceAll("\\D", ""))The example: PUBLIC class Example { public static void main(String[] args) { // string with digits String s = "12345example"; System.out.println("Our String = "+s); // display digits System.out.println("Get digits from the string = "+s.replaceAll("\\D", "")); } }The output displays only the digits: Our String = 12345example Get digits from the string = 12345 |
|