Java ProgramsStringsRemove Digits from a String

Remove Digits from a String in Java

beginner·  Strings  ·  String Manipulation

Problem

A digit is any of the characters 0 through 9 that can appear inside a string alongside letters and symbols.

Given a string, remove every digit it contains.

Input
Order66Confirmed42
Output
OrderConfirmed

Java Program

Java
public class RemoveDigits { public static void main(String[] args) { String str = "Order66Confirmed42"; StringBuilder result = new StringBuilder(); for (char c : str.toCharArray()) { if (!Character.isDigit(c)) result.append(c); // skip digits entirely } System.out.println(result.toString()); } }

Output

OrderConfirmed

Core Logic

Keeping every character that isn't a digit, one pass through the string, filters out the digits and leaves everything else in place.

How It Works
  1. 1A for-each loop visits each character of the string in turn.
  2. 2Character.isDigit(c) checks whether the current character is one of 0 through 9.
  3. 3Characters that fail the check are appended to a StringBuilder; digits are simply skipped.
  4. 4The final StringBuilder holds every original character except the digits.
For "Order66Confirmed42", the digits 6, 6, 4, and 2 are skipped, producing "OrderConfirmed".
💡

Key Point: This mirrors the digit-counting program exactly, just appending non-digits to a result instead of incrementing a counter for digits.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: Each character is visited once and the result buffer grows to hold every non-digit character, which can be up to n.

Key Concepts

Character.isDigit()StringBuilderfor-each loop

Approach 2: Java 8

Java
import java.util.stream.Collectors; public class RemoveDigitsStream { public static void main(String[] args) { String str = "Order66Confirmed42"; // Keeps every character that isn't a digit, then joins them back into a String String result = str.chars() .filter(c -> !Character.isDigit(c)) .mapToObj(c -> String.valueOf((char) c)) .collect(Collectors.joining()); System.out.println(result); } }

Output

OrderConfirmed

Core Logic

The same is-not-a-digit check can filter a stream of character codes, then join what's left back into a string.

How It Works
  1. 1str.chars() returns an IntStream of the string's character codes.
  2. 2.filter(c -> !Character.isDigit(c)) keeps only the codes that are NOT a digit.
  3. 3.mapToObj(c -> String.valueOf((char) c)) converts each surviving code back into a one-character String.
  4. 4.collect(Collectors.joining()) concatenates them all back into a single result string.
Filtering "Order66Confirmed42" drops every digit, and joining what's left reassembles "OrderConfirmed".
💡

Key Point: Negating Character::isDigit with ! flips the same check the loop version uses directly — streams just change how it's applied.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: The stream still visits every character once, and Collectors.joining() builds a result string holding every non-digit character.

Key Concepts

Streamchars()filter()Collectors.joining()

Related Programs