WAP to Remove Digits Only From a String

Remove Digits Only From a String: This Java program aims to remove numbers from a given input string and display the resulting string. It utilizes the Scanner class to read user input from the console and then uses the replaceAll() method of the String class to remove all occurrences of numbers from the input.

Remove Digits Only From a String

package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class InterviewPrograms92 
{
	//Remove numbers from a String Java using the replaceAll( ) method
	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);
	}
}

Output

Enter any String to remove numbers: 12Software34Testingo56
String after removing all numbers: SoftwareTestingo

Alternative Way 1:

This Java program aims to remove numbers from a given input string and display the resulting string. It utilizes the Scanner class to read user input from the console and then uses a loop with the charAt() method of the String class to process each character and build the resulting string without numbers.

package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class InterviewPrograms92_1 
{
	//Remove numbers from a String Java using charAt( ) method
	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);

	}
}

Output

Enter any String to remove numbers: 12Software34Testingo56
String after removing all numbers: SoftwareTestingo

Alternative Way 2:

This Java program aims to remove numbers from a given input string using regular expressions (regex) and display the resulting string. It utilizes the Scanner class to read user input from the console. Then, it uses the replaceAll() method with a regex pattern to remove all occurrences of numbers from the input.

package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class InterviewPrograms92_2 
{
	//Remove numbers from a String Java using Regx
	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);
	}
}

Output

Enter any String to remove numbers: 12Software34Testingo56
String after removing all numbers: SoftwareTestingo

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment