3 Simple Ways To Remove Numbers From String Java

In this post, we have given 3 simple ways to remove numbers from String Java. We have used replaceAll( ) method, regex and chatAt( ) method.

Logic Behind Me:

We just need to find particular numbers or digits from the given string and remove them from the same String to perform this program.

To remove specific numbers or digits, we can use replaceAll( ) method, regex or charAt( ) method.

Why do we need to remove numbers from String Java?

We can remove numbers from a String java by using replaceAll( ) method, regex and charAt( ) method.

Let’s see all approaches to remove numbers from a String:

  1. Remove numbers from a String Java using replaceAll( ) method.
  2. Remove numbers from a String Java using regex.
  3. Remove numbers from a String Java using charAt( ).

Also Read: How To Remove Particular Character From String In Java

Approach-1: Remove numbers from a String Java using the replaceAll( ) method

In this approach, we will simply replace all numbers or digits with replaceAll( ) method.

Let’s see the program, how we can remove numbers from a String by using replaceAll( ) method.

/**
 * How to remove numbers from String Java?
 */
package cf.java.string;

import java.util.Scanner;

public class RemoveNumbersFromAString {

	public static void main(String[] args) {
		String str;
		
		Scanner sc = new Scanner(System.in);
		// Accept any String to remove numbers
		System.out.print("Enter any String to remove numbers: ");
		str = sc.nextLine();
		
		// Replace all numbers from given String
		str = str.replaceAll("[0123456789]", "");
		// Display String without numbers
		System.out.print("String after removing all numbers: " + str);
	}

}
Code language: PHP (php)

Output:

Enter any String to remove numbers: Codingface123.com
String after removing all numbers: Codingface.comCode language: JavaScript (javascript)

Explanation:

In the above approach, We have just accepted any String from the user. Here we have taken the example of Codingface123.com. Here the number is 123. So to remove this number 123, we have used replaceAll( ) method as str.replaceAll(“[0123456789]”, “”);. It can replace all numbers including 123 from the given String.

You can take any example of String to test this program. Finally, we have displayed the given String without numbers.

Also Read: How To Check String Contains Special Characters In Java?

Approach-2: Remove numbers from a String Java using regex

In this approach, we will simply replace all numbers or digits with regex “\d”.

Let’s see the program, how we can remove numbers from a String by using regex “\d”.

/**
 * How to remove numbers from String Java using charAt( ) and isDigit( ) methods?
 */
package cf.java.string;

import java.util.Scanner;

public class RemoveNumbersFromAStringUsingRegex {

	public static void main(String[] args) {
		String str, res="";
		
		Scanner sc = new Scanner(System.in);
		// Accept any String to remove numbers
		System.out.print("Enter any String to remove numbers: ");
		str = sc.nextLine();

		for(int i=0; i<str.length(); i++) {
			if(!Character.isDigit(str.charAt(i))) {
				res += str.charAt(i);
			}
		}
		// Display String without numbers
		System.out.print("String after removing all numbers: " + res);
	}

}
Code language: PHP (php)

Output:

Enter any String to remove numbers: Codingface123.com
String after removing all numbers: Codingface.comCode language: JavaScript (javascript)

Explanation:

In the above approach, We have used a regex to remove numbers. We have used regex “\\d” as str.replaceAll(“\\d”, “”);. It can replace all numbers including 123 from the given String.

You can take the example of any String to test this program. Finally, we have displayed the given String without numbers.

Also Read: Simple Java Program To Print Vowels In A String

Approach-3: Remove numbers from a String Java using charAt( ) method

In this approach, we will simply replace all numbers or digits with charAt( ) method.

Let’s see the program, how we can remove numbers from a String by using charAt( ) method.

/**
 * How to remove numbers from String Java?
 */
package cf.java.string;

import java.util.Scanner;

public class RemoveNumbersFromAStringUsingCharAt {

	public static void main(String[] args) {
		String str;
		
		Scanner sc = new Scanner(System.in);
		// Accept any String to remove numbers
		System.out.print("Enter any String to remove numbers: ");
		str = sc.nextLine();
		
		// Replace all numbers from given String
		str = str.replaceAll("[0123456789]", "");
		// Display String without numbers
		System.out.print("String after removing all numbers: " + str);
	}

}
Code language: PHP (php)

Output:

Enter any String to remove numbers: Codingface123.com
String after removing all numbers: Codingface.comCode language: JavaScript (javascript)

Explanation:

In the above approach, We have used a both charAt( ) and isDigit( ) methods to remove numbers. We have these methods with if condition as if(!Character.isDigit(str.charAt(i))). It can replace all numbers including 123 from the given String.

You can take the example of any String to test this program. Finally, we have displayed the given String without numbers.

Leave a Comment