In this tutorial will guide you to learn how to remove digit from a string in java.
In this tutorial we will learn hoW to Remove all the digits from a string in Java with sum cool and easy examples.
In many situation, you might have to come up with this type of requirement .
I know you are here just because you are in need of this awesome trick to Remove all the digits from a string in Java . if you don’t know how to Remove all tha digits from a string then you are at the right place. Becoause in this tutorial we gonna Remove all the digits from a string in Java.
Let’s learn this with some easy examples,
At first we create a string which can hold the value entered by the user in java for remove digit from a string.(user input)
import java.util.*; class Remove_Digit { public static void main(String[] args) { Scanner sc= new Scanner(System.in); System.out.print("Enter any string: "); String s= sc.nextLine(); System.out.println("You have entered: " + s); } }
How to Remove Numeric values from String in Java
import java.util.*; public class Remove_Digit { public static void main(String[] args) { Scanner sc=new Scanner (System.in); System.out.println(" enter any string"); String s=sc.nextLine(); String p= "" ; char ch; for(int i=0;i<s.length();i++) { ch=s.charAt(i); if ((ch>=48 &&ch<=57)) { continue; } else{ p= p+ch; } } System.out.println( "RemoveDigit= " + p); } }
Output:
Enter your string:
Input
Raj6297 Agrahari147258 BCET108
Output :
RemoveDigit= Raj Agrahari BCET
Summary :
- Get the String to remove all the digit.
Convert the given string into a character array.
Initialize an empty string that stores the result.
Traverse the character array from start to end.
Check if the specified character is not digit then add this character into the result variable.
Now, print .
A string is a collection of an alphabetical arrangement of all the 26 available characters in the English Language and also can contain sum digit, numbers or numeric values and special characters.
Whereas the decimal number system is a collection of all the digits ranging from (zero) 0 to (nine) 9.