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); } }
Please refer to the following string programs.
- Count the Total Occurrence of a Character in a String
- Find All Occurrences of a Character in a String
- First Character Occurrence in a String
- Replace First Character Occurrence in a String
- Remove First Character Occurrence in a String
- Last Character Occurrence in a String
- Replace Last Character Occurrence in a String
- Remove the Last Character Occurrence in a String
- First and Last Character in a String
- Remove The First and Last Character in a String
- Maximum Occurring Character in a String
- Minimum Occurring Character in a String
- Frequency of each Character in a String