Java Program to Remove Last Character Occurrence in a String

Write a Java Program to remove or delete the last Character occurrence in a String with an example. In this Java Remove the last occurrence of a character example, we used the StringBuilder lastIndexOf and deleteCharAt functions.

To get the last index position of delLastCharStr, we used lastIndexOf, delLastCharStr.lastIndexOf(del_lch) that returns the last index position. Next, we used sb.deleteCharAt(i) to delete or remove that character from sb. Here, we can also use i = sb.lastIndexOf(del_lch);

import java.util.Scanner;

public class DeleteLastCharOcc1 {
	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 Last Character =  ");
		delLastCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Delete =  ");
		del_lch = sc.next().charAt(0);
		
		StringBuilder sb = new StringBuilder(delLastCharStr);
		int i = delLastCharStr.lastIndexOf(del_lch);
		sb.deleteCharAt(i);
		
		System.out.println("\nThe Final String after Deleting Last Occurrence of " + 
				del_lch + " Character = " + sb);
	}
}

Java Remove Last Character Occurrence in a String output

Please Enter String to Delete Last Character =  Java

Enter the Character to Delete =  a

The Final String after Deleting Last Occurrence of a Character = Jav

The Java StringBuffer also has the lastIndexOf and deleteCharAt functions to remove the last occurrence of a character in a string.

import java.util.Scanner;

public class DeleteLastCharOcc2 {
	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 Last Character =  ");
		delLastCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Delete =  ");
		del_lch = sc.next().charAt(0);
		
		StringBuffer sbuff = new StringBuffer(delLastCharStr);
		int i = delLastCharStr.lastIndexOf(del_lch);
		sbuff.deleteCharAt(i);
		
		System.out.println("\nThe Final String after Deleting Last Occurrence of " + 
		del_lch + " = " + sbuff);
	}
}
Please Enter String to Delete Last Character =  tutorial

Enter the Character to Delete =  t

The Final String after Deleting Last Occurrence of t = tuorial

Java Program to Remove Last Character Occurrence in a String Example 3

In this Java remove the last occurrence of a Character example, we used For loop to iterate delLastCharStr from end to start. Within the loop, we are comparing delLastCharStr characters with del_lch. If they are equal, the break statement will exit the for loop. Next, we used the StringBuilder and StringBuffer deleteCharAt function to remove the last character.

import java.util.Scanner;

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

		System.out.print("\nPlease Enter String to Delete Last Character =  ");
		delLastCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Delete =  ");
		del_lch = sc.next().charAt(0);
		
		StringBuilder sb = new StringBuilder(delLastCharStr);
		StringBuffer sbuff = new StringBuffer(delLastCharStr);
		
		for(i = delLastCharStr.length() - 1; i >= 0; i--) {
			if(delLastCharStr.charAt(i) == del_lch) {
				break;
			}
		}		
		System.out.format("\nThe Last Character Occurence of %c at %d position", del_lch, i);
		
		sb.deleteCharAt(i);		
		System.out.println("\n\nThe Final String after Deleting Last Occurrence of " + 
				del_lch + " Character = " + sb);
		
		sbuff.deleteCharAt(i);	
		System.out.println("The Final String after Deleting Last Occurrence of " 
		+ del_lch + " = " + sbuff);
	}
}
Please Enter String to Delete Last Character =  programming language

Enter the Character to Delete =  g

The Last Character Occurence of g at 18 position

The Final String after Deleting Last Occurrence of g Character = programming languae
The Final String after Deleting Last Occurrence of g = programming languae

To remove the last character occurrence, we can also use the Java String substring function. In this Java program, delLastCharStr.lastIndexOf(del_lch) finds the index position of last character occurrence. Next, delLastCharStr.substring(0, i) + delLastCharStr.substring(i + 1) returns string without the character at index position i.

import java.util.Scanner;

public class DeleteLastCharOcc4 {
	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 Last Character =  ");
		delLastCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Delete =  ");
		del_lch = sc.next().charAt(0);
		
		int i = delLastCharStr.lastIndexOf(del_lch);
		String out = delLastCharStr.substring(0, i) + delLastCharStr.substring(i + 1);
		
		System.out.println("\nThe Final String after Deleting Last Occurrence of " + 
				del_lch + " Character = " + out);
	}
}
Java Program to Remove Last occurrence of a Character in a String 4

In this Java delete the last occurrence of a character example, we converted the delLastCharStr string to delLastCharArr char array using the toCharArray(). Next, within the for loop, we are comparing the delLastCharStr string character with del_lch. If they are equal, we assigned 0 or empty characters to that delLastCharArr index position followed by a Java break statement to exit from for loop.

import java.util.Scanner;

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

		System.out.print("\nPlease Enter String to Delete Last Character =  ");
		delLastCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Delete =  ");
		del_lch = sc.next().charAt(0);
		
		char[] delLastCharArr = delLastCharStr.toCharArray();
		len = delLastCharArr.length;
		
		for(i = len - 1; i >= 0; i--) {			
			if(delLastCharArr[i] == del_lch) {
				delLastCharArr[i] = 0;
				break;
			}
		}

		System.out.print("\nThe Final String after Deleting Last Occurrence of " + 
				del_lch + " Character = " );
		System.out.print(delLastCharArr);
		
	}
}
Please Enter String to Delete Last Character =  hello world

Enter the Character to Delete =  l

The Final String after Deleting Last Occurrence of l Character = hello word

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. First and Last Character in a String
  9. Remove The First and Last Character in a String
  10. Maximum Occurring Character in a String
  11. Minimum Occurring Character in a String
  12. Frequency of each Character in a String
  13. Remove All Occurrences of a Character in a String