Java program to remove characters in a string except alphabets

Remove character in a String except alphabet

In this article we will learn how to Remove character in a String except alphabet.

Take String input from the user and store it in a variable called “s”(in this case). After that use replaceAll() method which was present in string class the work of replaceAll() method is to replace some old thing with some new thing so here we’re using a regular expression to replace character in a string with blank spaces.

Java program to remove characters in a string except alphabets

Algorithm

  • Take String input from user and store it in a variable called “s”.
  • After that use replaceAll() method.
  • Write regex to replace character with whitespaces like this s.replaceAll(“[^a-zA-Z]”,””);.
  • After that simply print the String after removing character except alphabet.

Code in Java

Output

helloworld

Method 2

Run
class Main
{
    // function to remove characters and
    // print new string
    static void removeSpecialCharacter(String s)
    {
        for (int i = 0; i < s.length(); i++)
        {
            // Finding the character whose
            // ASCII value fall under this
            // range
            if (s.charAt(i) < 'A' || s.charAt(i) > 'Z' && s.charAt(i) < 'a' || s.charAt(i) > 'z')
            {

                // erase function to erase
                // the character
                s = s.substring(0, i) + s.substring(i + 1);
                i--;
            }
        }
        System.out.print(s);
    }
    // Driver code
    public static void main(String[] args)
    {
        String s = "$P*r;e..pi, ns'ta^?";
        removeSpecialCharacter(s);
    }
}

Output

Prepinsta
Run
import java.util.Scanner;

class Main {

public static void main(String[] args) {
     String s = "hel1456lo56wor%^ld";
     s=s.replaceAll("[^a-zA-Z]","");
     System.out.println(s);
   }
}

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

18 comments on “Java program to remove characters in a string except alphabets”


  • Sohail

    import java.util.Scanner;

    public class Remove_spCharacter {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s = sc.next();
    String ans = “”;
    for (int i=0;i<s.length();i++){
    char c = s.charAt(i);
    if (c=’Z’ && c=’z’){
    ans+=””;
    }
    else {
    ans+=c;
    }
    }
    System.out.println(ans);
    }
    } that is correct


  • Sohail

    import java.util.Scanner;

    public class Remove_spCharacter {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s = sc.next();
    String ans = “”;
    for (int i=0;i<s.length();i++){
    char c = s.charAt(i);
    if (c=’Z’ && c=’z’){
    ans+=””;
    }
    else {
    ans+=c;
    }
    }
    System.out.println(ans);
    }
    }


  • mstofficial19

    public static void main(String[] args) {
    System.out.println(“Enter a String: “);
    Scanner sc=new Scanner(System.in);
    String str=sc.next();
    String answer=””;
    for(int i=0;i=65&str.charAt(i)=97&str.charAt(i)<=122) {
    answer+=str.charAt(i);
    }
    }
    System.out.println("After processing: ");
    System.out.println(answer);

    }


  • VIGNESHWARAN S 20ita62

    //another method using wrapper class
    import java.util.*;
    public class onlyalphabets {
    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    String s=sc.nextLine();
    String res=” “;
    for(int i=0;i<s.length();i++)
    {
    char c=s.charAt(i);
    if(Character.isAlphabetic(c))
    {
    res+=c;
    }
    }
    System.out.println(res);
    }

    }


  • Gyanendra

    import java.util.*;
    public class Main
    {
    public static void main (String[]args)
    {
    String str=”12@45Gyanen9dra85Tiwari”;

    System.out.println(find(str));
    }

    public static StringBuffer find(String str){

    StringBuffer ans=new StringBuffer();

    for(int i=0;i=’A’ && ch=’a’ && ch<='z')){
    ans.append(ch);
    }

    }

    return ans;
    }
    }


  • Ziaul

    public static void remove(String str) {
    char ch[]=str.toCharArray();
    String rem=””;
    for(int i=0;i<str.length();i++) {
    if(Character.isUpperCase(ch[i]) || Character.isLowerCase(ch[i])) {
    rem=rem+ch[i];
    }
    }
    System.out.println(rem);
    }


  • Ziaul

    public static void remove(String str) {
    char ch[]=str.toCharArray();
    String rem=””;
    for(int i=0;i<str.length();i++) {

    if(Character.isUpperCase(ch[i]) || Character.isLowerCase(ch[i])) {
    rem=rem+ch[i];
    }
    }
    System.out.println(rem);
    }


  • Vijaykhede

    public class Main {
    public static void main(String[] args){
    System.out.println(“enter a string”);
    Scanner sc=new Scanner(System.in);
    String s= sc.next();
    String s2=””;
    int i;
    for(i=0;i=’a’&&ch=’A’&&ch<='Z')
    s2=s2+(char)ch;
    }
    s=s2;
    System.out.println(s);
    }
    }


  • Priyanka

    import java.util.*;
    import java.lang.*;
    public class Main
    {
    public static void main(String[] args)
    {
    Scanner sc =new Scanner(System.in);
    System.out.print(“Enter String : “);
    String s = sc.nextLine();
    s=s.replaceAll(“[0-9]”,””);
    System.out.println(s);
    }
    }


  • Vaibhav

    package stringprogram;

    import java.util.Scanner;

    public class RemoveCharactersInAtringExceptAlphabetsWithoutUsingApi {

    public static void main(String[] args)
    {

    Scanner sc =new Scanner(System.in);
    System.out.print(“Enter String : “);
    String Str1 = sc.nextLine();
    String Str2=””;
    for(int i=0;i=65 && (int)Str1.charAt(i)=97 && (int)Str1.charAt(i) “+Str2);

    }

    }