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

Step-by-Step Explanation:

  • The program starts by importing the required class, Scanner.
  • It defines a public class named InterviewPrograms92.
  • The main method serves as the entry point of the program.
  • A String variable named str is declared to store the input string.
  • It creates a new Scanner object (sc) to read user input from the console.
  • The user is prompted to enter a string using System.out.print(“Enter any String to remove numbers: “);.
  • The input string is read from the user using str = sc.nextLine();.
  • The program prints the original input string using System.out.print(“String after removing all numbers: ” + str);.
  • The replaceAll() method is used to remove all occurrences of numbers from the input string. The regex pattern [0123456789] specifies that any digit from 0 to 9 should be replaced with an empty string, effectively removing all numbers from the string.
  • The modified string with numbers removed is printed on the console using System.out.print(“String after removing all numbers: ” + str);.

Summary: The Java program allows users to input a string and then uses the replaceAll() method to remove all occurrences of numbers from the input. The original input string and the modified string (with numbers removed) are displayed on the console.

This program is helpful for beginners to understand how to read user input, use the replaceAll() method to manipulate strings using regex patterns, and achieve the task of removing numbers from a string in Java. It demonstrates a simple and efficient approach to remove all numerical digits from a string by replacing them with an empty string.

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

Step-by-Step Explanation:

  • The program starts by importing the required class, Scanner.
  • It defines a public class named InterviewPrograms92_1.
  • The main method serves as the entry point of the program.
  • Two String variables, str and res, are declared. str is used to store the input string, and res will be used to store the resulting string without numbers.
  • It creates a new Scanner object (sc) to read user input from the console.
  • The user is prompted to enter a string using System.out.print(“Enter any String to remove numbers: “);.
  • The input string is read from the user using str = sc.nextLine();.
  • A for loop is used to iterate through each character of the input string.
  • Inside the loop, the charAt() method is used to access each character at index i.
  • The Character.isDigit() method is used to check if the current character is not a digit (i.e., not a number).
  • If the character is not a digit, it is added to the res string using res += str.charAt(i);.
  • After processing all characters, the modified string without numbers (stored in res) is printed on the console using System.out.print(“String after removing all numbers: ” + res);.

Summary: The Java program allows users to input a string and then manually processes each character to remove numbers from the input. It uses the charAt() method to access characters and the Character.isDigit() method to check if a character is a digit. The original input string and the modified string (with numbers removed) are displayed on the console.

This program is helpful for beginners to understand how to read user input, use loops and character manipulation to remove specific characters from a string, and achieve the task of removing numbers from a string in Java. It demonstrates an alternative approach to remove numbers by explicitly examining each character and building a new string without numbers.

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 and then 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

Step-by-Step Explanation:

  • The program starts by importing the required class, Scanner.
  • It defines a public class named InterviewPrograms92_2.
  • The main method serves as the entry point of the program.
  • A String variable named str is declared to store the input string.
  • It creates a new Scanner object (sc) to read user input from the console.
  • The user is prompted to enter a string using System.out.print(“Enter any String to remove numbers: “);.
  • The input string is read from the user using str = sc.nextLine();.
  • The program prints the original input string using System.out.print(“String after removing all numbers: ” + str);.
  • The replaceAll() method is used to remove all occurrences of numbers from the input string. The regex pattern [0123456789] specifies that any digit from 0 to 9 should be replaced with an empty string, effectively removing all numbers from the string.
  • The modified string with numbers removed is printed on the console using System.out.print(“String after removing all numbers: ” + str);.

Summary: The Java program allows users to input a string and then uses the replaceAll() method with a regex pattern to remove all occurrences of numbers from the input. The original input string and the modified string (with numbers removed) are displayed on the console.

This program is helpful for beginners to understand how to read user input, use the replaceAll() method with regex patterns to manipulate strings and achieve the task of removing numbers from a string in Java using a regex approach. It demonstrates a simple and concise way to remove all numerical digits from a string by replacing them with an empty string using regular expressions.

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