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 Java, there is a string replace function that replaces all the occurrences of a given character with a new character. In this Java 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);
	}
}

Java remove all character occurrences in a string output

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 Java remove all Character Occurrences example, delLastCharStr.toCharArray() converts the delLastCharStr to delLastCharArr char array. Next, we used the for loop to iterate delLastCharArr 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 3

Please refer to the following string programs.

  1. Count the Total Occurrence of a Character in a String
  2. Find All Occurrences of a Character in a String
  3. First Character Occurrence in a String
  4. Replace First Character Occurrence in a String
  5. Remove First Character Occurrence in a String
  6. Last Character Occurrence in a String
  7. Replace Last Character Occurrence in a String
  8. Remove the Last Character Occurrence in a String
  9. First and Last Character in a String
  10. Remove The First and Last Character in a String
  11. Maximum Occurring Character in a String
  12. Minimum Occurring Character in a String
  13. Frequency of each Character in a String