Given a string str consisting of digits, alphabets and special characters. The task is to extract all the integers from the given string.
Input: str = “geeksforgeeks A-118, Sector-136, Uttar Pradesh-201305”
Output: 118 136 201305
Input: str = ” 1abc35de 99fgh, dd11″
Output: 1 35 99 11
- Replace all the non-digit characters with spaces (” “).
- Now replace every consecutive group of spaces with a single space.
- Eliminate the leading and the trailing spaces (if any) and the final string will only contain the required integers.
Below is the implementation of the above approach:
1122 4 543
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Extract all integers from string in C++
- Program to extract words from a given String
- Python Regex to extract maximum numeric value from a string
- Extract maximum numeric value from a given string | Set 1 (General approach)
- Extract maximum numeric value from a given string | Set 2 (Regex approach)
- Extract and print words separately from a given Camel Case string
- Number of substrings divisible by 6 in a string of integers
- Count of integers obtained by replacing ? in the given string that give remainder 5 when divided by 13
- Generate Infinite Stream of Integers in Java
- How do I generate random integers within a specific range in Java?
- Convert a List of String to a comma separated String in Java
- Collator compare(String, String) method in Java with Example
- Convert a Set of String to a comma separated String in Java
- Collator equals(String, String) method in Java with Example
- Convert an ArrayList of String to a String array in Java
- String Literal Vs String Object in Java
- Convert Set of String to Array of String in Java
- Java.lang.string.replace() method in Java
- Insert a String into another String in Java
- Java.lang.String.copyValueOf() in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.