Java Program to Remove All Occurrences of a Character in a String

Write a Java Program to Remove All Occurrences of a Character in a String with an example. In this programming language, there is a string replace function that replaces all the occurrences of a given character with a new character. In this program, delLastCharStr.replace(del_lch, Character.MIN_VALUE); replace del_lch with minimum character that is empty.

import java.util.Scanner;

public class DeleteAllCharOcc1 {
	private static Scanner sc;
	public static void main(String[] args) {
		String delLastCharStr;
		char del_lch;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to Delete All Characters =  ");
		delLastCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Delete =  ");
		del_lch = sc.next().charAt(0);
		
		String out = delLastCharStr.replace(del_lch, Character.MIN_VALUE); 
		
		System.out.println("\nThe Final String after Deleting " + 
				del_lch + " Characters = " + out);
	}
}

Remove all character occurrences in a string output is

Please Enter String to Delete All Characters =  java programming

Enter the Character to Delete =  a

The Final String after Deleting a Characters = jv progrmming

In this remove all Character Occurrences example, delLastCharStr.toCharArray() converts the delLastCharStr to delLastCharArr char array. Next, we used the for loop to iterate delLastCharArr Java array from start to end.

Within that, we compared delLastCharArr[I] (each character) with del_lch character to check whether they are equal. If True, we replace that character with an empty character, followed by a continue statement to continue the loop.

import java.util.Scanner;

public class DeleteAllCharOcc2 {
	private static Scanner sc;
	public static void main(String[] args) {
		String delLastCharStr;
		char del_ch;
		int i; 
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to Delete All Characters =  ");
		delLastCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Delete =  ");
		del_ch = sc.next().charAt(0);
		
		char[] delLastCharArr = delLastCharStr.toCharArray();
		
		for(i = 0; i < delLastCharArr.length; i++) {
			if(delLastCharArr[i] == del_ch) {
				delLastCharArr[i] = '\0';
				continue;
			}
		}
	
		System.out.println("\nThe Final String after Deleting " + 
				del_ch + " Character = " );
		System.out.print(delLastCharArr);
	}
}
Please Enter String to Delete All Characters =  tutorial gateway

Enter the Character to Delete =  t

The Final String after Deleting t Character = 
uorial gaeway

Java Program to Remove All Occurrences of a Character in a String using While loop

import java.util.Scanner;
public class DeleteAllCharOcc3 {
	private static Scanner sc;
	public static void main(String[] args) {
		String delLastCharStr;
		char del_ch;
		int i = 0; 
		
		sc= new Scanner(System.in);
		System.out.print("\nPlease Enter String to Delete All Characters =  ");
		delLastCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Delete =  ");
		del_ch = sc.next().charAt(0);		
		char[] delLastCharArr = delLastCharStr.toCharArray();
		
		while(i < delLastCharArr.length) {
			if(delLastCharArr[i] == del_ch) {
				delLastCharArr[i] = 0;
				continue;
			}
			i++;
		}
		System.out.print("\nThe Final String after Deleting " + 
				del_ch + " Character = " );
		System.out.print(delLastCharArr);
	}
}
Java Program to delete All Occurrences of a Character in a String