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
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Extract all integers from string in C++
- Program to extract words from a given 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
- Java Program to Extract Content from a Java's .class File
- Java Program to Extract Last two Digits of a Given Year
- Java Program to Extract Digits from A Given Integer
- Python Regex to extract maximum numeric value from a string
- Java Program to Extract a Image From a PDF
- Java Program to Extract Content From a XML Document
- Count of integers obtained by replacing ? in the given string that give remainder 5 when divided by 13
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Number of substrings divisible by 6 in a string of integers
- Minimum length of string having all permutation of given string.
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Generate Infinite Stream of Integers in Java
- How do I generate random integers within a specific range in Java?
- Check if Two Integers are Equal or Not in Java
- Count of integers of length N and value less than K such that they contain digits only from the given set
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.