Use this code numberOnly will contain your desired output.

   String str="sdfvsdf68fsdfsf8999fsdf09";
   String numberOnly= str.replaceAll("[^0-9]", "");
Answer from user1999257 on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › extract-all-integers-from-the-given-string-in-java
Extract all integers from the given String in Java - GeeksforGeeks
Apart from regular expressions, there are other effective methods to extract integers from a string. We will dicuss them below: This method iterates through each character in the string by identifying numeric characters, and building numbers step by step. ... // Java program to extract integers from a given string public class Extract { public static void main(String[] args) { String s = "abc123def456ghi"; StringBuilder r = new StringBuilder(); boolean wasDigit = false; // Flag to track if the previous character was a digit // Iterate through each character in the string for (char c : s.toChar
Published: July 12, 2025
Discussions

Extracting mixed number and first string from string [java]
The possibility of there being no number OR unit complicates things. What I would do is: Use regex to match and extract a leading number, if there is one. See here . Have a pre-created list of possible units (you'll have to make it yourself) and compare them against remaining text. If you get a match, you found the unit. You'll have to make sure that your list of units is comprehensive (and check case insensitive). Use the match method of String to check if a match exists. The regex for the number will take some thought, since you must be able to accept whole numbers, real numbers, fractions, and mixed numbers. I won't do this for you. It's not too difficult as far as regex goes. Test this heavily before you continue with step 2. More on reddit.com
🌐 r/learnprogramming
5
1
October 24, 2014
Java Regular Expression, extract 5 digit numbers from a string
String Input looks like : C:\FW INV ORDER - 35425 35426 35427 35428 35429 35430 35431 35432.msg My output looks like: arr: [35425, 35427, 35429, 35431] Arr Size: 4 So why is it not extracting all 5 digit numbers? Li… More on forum.knime.com
🌐 forum.knime.com
1
0
December 24, 2019
Extract number from a string
Hi all, i doing an automation testing using katalon. I need to know how to extract ONLY NUMBER from a string. For instance i wanted to extract ‘1421’ from “Application number: 1421” from the above page. The application number changes for every single testing. More on forum.katalon.com
🌐 forum.katalon.com
19
0
March 18, 2019
java - regex - Extract number from string. Number should be first 5 digit from my input - Stack Overflow
I am new to regix. I want to get only port number from following line: Input: Install: C:\Program Files\app Database: postgresql://127.0.0.1:42018/app Started: 2016-12-28 10:40:05.908000 Lines: 1... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Medium
medium.com › @sujathamudadla1213 › how-to-extract-numbers-from-a-long-string-in-java-b61ccbf85b76
How to extract numbers from a long string in Java | by Sujatha Mudadla | Medium
July 17, 2023 - Using String Manipulation and Parsing: In this approach, you can split the string into words and then parse each word to check if it represents a number. public class ExtractNumbersFromString { public static void main(String[] args) { String ...
🌐
Baeldung
baeldung.com › home › java › java string › find all numbers in a string in java
Find All Numbers in a String in Java | Baeldung
June 24, 2025 - If a decimal number is negative, it starts with a minus sign. This is followed by one or more digits and an optional fractional part. This fractional part starts with a decimal point, with another sequence of one or more digits after that. We can define this using the regular expression “-?\d+(\.\d+)?“: List<String> findDecimalNums(String stringToSearch) { Pattern decimalNumPattern = Pattern.compile("-?\\d+(\\.\\d+)?"); Matcher matcher = decimalNumPattern.matcher(stringToSearch); List<String> decimalNumList = new ArrayList<>(); while (matcher.find()) { decimalNumList.add(matcher.group()); } return decimalNumList; }
🌐
LinkedIn
linkedin.com › pulse › java-extract-number-from-string-rajesh-k
Java extract number from string
January 25, 2018 - Java extract number from string ... etc.In order to work or extract numbers from String we are going to use regular expression and replaceAll method in Java....
🌐
Quora
quora.com › How-do-you-extract-numbers-from-a-string-in-Java
How to extract numbers from a string in Java - Quora
Answer (1 of 3): You can extract numbers from a string in Java using two methods. Method 1: Using the in-built method Character.isDigit() The Character.isDigit() method determines whether the character is a digit or not. So you traverse the string, get each character one by one and check if it ...
🌐
DevQA
devqa.io › extract-numbers-string-java-regular-expressions
Extract Numbers From String Using Java Regular Expressions
February 11, 2020 - import java.util.regex.Matcher; ... } } ... If you want to extract only certain numbers from a string you can provide an index to the group() function....
Find elsewhere
🌐
Reddit
reddit.com › r/learnprogramming › extracting mixed number and first string from string [java]
r/learnprogramming on Reddit: Extracting mixed number and first string from string [java]
October 24, 2014 -

Say I have a string (ingAmntTemp) with a number followed by a unit of measure (ex. 1 1/2 cups). I need to split the number from the unit of measure and store both into separate variables (ingUnit and ingNum)... how would I do this?

The problem is that the number may be there, but not the unit. Also, the number may have a whole part but not a fractional part, or vice versa.

I know of regexes and I assume I need to use one here as an argument in .split() or .replaceAll(), but I have no idea how to go about doing it.

EDIT: Changed some mistakes

🌐
GeeksforGeeks
geeksforgeeks.org › dsa › extract-all-integers-from-a-given-string
Extract all integers from a given String - GeeksforGeeks
May 24, 2026 - // Java program to extract integers from string without regex import java.util.*; class GfG { // Function to extract integers from the string static List<String> extractInt(String str) { List<String> ans = new ArrayList<>(); String num = ""; ...
🌐
YouTube
youtube.com › watch
How to extract numbers or alphabets from a String in Java? - YouTube
In this video, we show **how to extract numbers or alphabets from a String in Java** using simple and effective methods. What you’ll learn: ✔ Extract numbe...
Published: March 27, 2020
🌐
Tutorjoes
tutorjoes.in › Java_example_programs › extract_numbers_from_the_string_in_java
Write Java program to Extract Numbers from the string
replaceAll() method is an empty string, which replaces all non-numeric characters with nothing, effectively removing them from the string. The resulting string with only numeric characters is assigned to the ... System.out.println() method, along with a descriptive message. import ...
🌐
TutorialsPoint
tutorialspoint.com › article › java-program-to-extract-digits-from-a-given-integer
Java Program to Extract Digits from A Given Integer
May 31, 2024 - public class Main { public static void main(String[] args) { int num = 987654; String numString = String.valueOf(num); for (char digitChar : numString.toCharArray()) { if (Character.isDigit(digitChar)) { int digit = Character.getNumericValue(digitChar); System.out.println("Digit: " + digit); } } } } Digit: 9 Digit: 8 Digit: 7 Digit: 6 Digit: 5 Digit: 4 · In this tutorial, we explored the process of extracting digits from a given integer in Java.
🌐
Katalon
forum.katalon.com › web testing
Extract number from a string - Web Testing - Katalon Community
March 18, 2019 - Hi all, i doing an automation testing using katalon. I need to know how to extract ONLY NUMBER from a string. For instance i wanted to extract ‘1421’ from “Application number: 1421” from the above page. The application number changes for every single testing.
🌐
W3docs
w3docs.com › home › code snippets › java › check and extract a number from a string in java
Check and extract a number from a String in Java | W3docs
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) throws Exception { String s = "Hello, world! The number is 42."; // Check if the string contains a number Pattern p = Pattern.compile("\\d+"); Matcher m = p.matcher(s); if (m.find()) { // Extract the number from the string String number = m.group(); System.out.println("Number: " + number); // Outputs "Number: 42" } else { System.out.println("No number found."); } } }
🌐
CodeScracker
codescracker.com › java › program › java-extract-numbers-from-string.htm
Java Program to Extract Numbers from String
import java.util.Scanner; public class CodesCracker { public static void main(String[] args) { String str; char ch; int strLen, i, strIndex=0; Scanner s = new Scanner(System.in); System.out.print("Enter the String: "); str = s.nextLine(); strLen = str.length(); char[] nums = new char[strLen]; for(i=0; i<strLen; i++) { ch = str.charAt(i); if(Character.isDigit(ch)) { nums[strIndex] = ch; strIndex++; } } if(strIndex==0) System.out.println("\nNumber not found in the string!"); else if(strIndex==1) { System.out.println("\nThere is only one number found in the string."); System.out.println("And the number is: " +nums[0]); } else { System.out.println("\nNumbers found in the string are:"); for(i=0; i<strIndex; i++) System.out.print(nums[i]); } } } Here is its sample run with user input Java is Fun!
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 472619 › how-would-i-extract-number-out-of-a-string-after-a-character
java - How would I extract number out of a String ... | DaniWeb
February 4, 2014 - For example, if String s = "22+5+35" , how can I extract and store the numbers 22,5, and 35 in a ArrayList? ... Brief answer: if you only want the numeric tokens (22, 5, 35) use a regex matcher to find contiguous digit groups and parse each match into an Integer. This avoids empty tokens from splitting and handles multi-digit numbers cleanly — building on points made by @cgeier and @Taywin.
🌐
Techstreampulse
techstreampulse.in › selenium › extract-numbers-from-string-java
extract numbers from string – java
abstraction alert apt-get array arraylist arrays checked-exceptions chrome driver compare strings do while ... for example if you want to compare any phone number with actual value, 555-666-777 and want to avoid hyphens or what ever..and extract only numbers use replaceall with correct regular expressions.