Remove Specific Characters from String with Example

If we  are asked to remove specific or certain or unwanted characters from the string .How can we achieve this in the most efficient way ? This  question has cemented its place in the technical interview of the reputed companies like Amazon or Goldman sachs .

First we need to understand the question , we need to write an algorithm to remove the certain or specific or unwanted characters from the string , for example

Read Also :    Remove all whitespaces in the string

If the original string is "Alive is awesome"   and the user inputs string to remove "alwsr"  then it should print  "ive i eome" as output .

If the original string is "Learning never stops"   and the user inputs string to remove "estp"  then the it should print   "Larning nvr o" as output .

let us understand the logic and pseudo algorithm to achieve this task .

The prototype of the  function to achieve result will be  like this

String removeSpecificChars( String originalstring , String  removecharacterstring)

Logic :

We need to remove the certain characters from the original string . To achieve this , we will create a tempBoolean array , in which every index element value will be  false by default . As we know if we put char in integer field , it will always print the ascii value of the character . We will pass one by one  the characters of the removecharacterstring  to the  index of the tempBoolean array , and set the corresponding index value to true . Now iterate through the originalstring and print the character only if its value in tempBoolean is false.

Read Also :  Find first non repeated character in the string : Amazon interview question

Pseudo Algorithm :

*   Change strings  originalstring and removecharacterstring  to character array .

*   Create a temporary boolean array named   tempBoolean of length 128 (ASCII characters) . This will set
     all the indexes value to false  by default .

*   Iterate through each character in   removecharacterstring   , setting the corresponding values  in the
     tempBoolean  array to true .

*  Start iterating originalstring  from 0 to the length of the originalstring string , copying each character           only if  its corresponding value in tempBoolean array is false .

Read Also :     Reverse string without using built in java method 

Demo :

remove specific characters from the string in java

Code :

import java.util.Scanner;

public class RemoveSpecificCharacter {
    public static void main(String[] args)
        String originalstring="Alive is Awesome";
        System.out.println("Original string is >>  "+ originalstring);
        System.out.println("Please enter unwanted characters as String");
        Scanner in =new Scanner(;
        String removecharacterstring=in.nextLine();
        String output=removeSpecificChars(originalstring, removecharacterstring);
        System.out.print("Output is >>  " );
    public static String removeSpecificChars(String originalstring ,String removecharacterstring)
        char[] orgchararray=originalstring.toCharArray();
        char[] removechararray=removecharacterstring.toCharArray();
        int start,end=0;
        //tempBoolean automatically initialized to false ,size 128 assumes ASCII
        boolean[]  tempBoolean = new boolean[128];
        //Set flags for the character to be removed
        for(start=0;start < removechararray.length;++start)
        //loop through all characters ,copying only if they are flagged to false
        for(start=0;start < orgchararray.length;++start)
        return new String(orgchararray,0,end);

About The Author

Subham Mittal has worked in Oracle for 3 years .
For more, follow me @javahungry