Java Program to Remove First Character Occurrence in a String

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

To get the index position, we used indexOf, delFirstCharStr.indexOf(del_ch) that returns the first index position. Next, we used sb.deleteCharAt(i) to delete or remove that character from delFirstCharStr.

import java.util.Scanner;

public class DeleteFirstCharOcc1 {
	private static Scanner sc;
	public static void main(String[] args) {
		String delFirstCharStr;
		char del_ch;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to Delete First Character =  ");
		delFirstCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Delete =  ");
		del_ch = sc.next().charAt(0);
		
		StringBuilder sb = new StringBuilder(delFirstCharStr);
		int i = delFirstCharStr.indexOf(del_ch);
		sb.deleteCharAt(i);
		
		System.out.println("\nThe Final String after Deleting " + del_ch + " = " + sb);
	}
}
Java Program to Remove First Character Occurrence in a String

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

import java.util.Scanner;

public class DeleteFirstCharOcc2 {
	private static Scanner sc;
	public static void main(String[] args) {
		String delFirstCharStr;
		char del_ch;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to Delete First Character =  ");
		delFirstCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Delete =  ");
		del_ch = sc.next().charAt(0);
		
		StringBuffer sbuff = new StringBuffer(delFirstCharStr);
		int i = delFirstCharStr.indexOf(del_ch);
		sbuff.deleteCharAt(i);
		
		System.out.println("\nThe Final String after Deleting First Occurrence " 
		+ del_ch + " = " + sbuff);
	}
}

Java Remove First Character Occurrence in a String output

Please Enter String to Delete First Character =  java programs

Enter the Character to Delete =  a

The Final String after Deleting First Occurrence a = jva programs

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

In this Java remove the first occurrence of a Character example, we used For Loop to iterate delFirstCharStr from start to end. Within the Loop, we are comparing delFirstCharStr each character with del_ch. If they are equal, the break statement will exit the Javac from for Loop. Next, we used the StringBuffer and StringBuilder deleteCharAt function to remove that character.

import java.util.Scanner;

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

		System.out.print("\nPlease Enter String to Delete First Character =  ");
		delFirstCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Delete =  ");
		del_ch = sc.next().charAt(0);
		
		StringBuilder sb = new StringBuilder(delFirstCharStr);
		StringBuffer sbuff = new StringBuffer(delFirstCharStr);
		
		for(i = 0; i < delFirstCharStr.length(); i++) {
			if(delFirstCharStr.charAt(i) == del_ch) {
				break;
			}
		}		
		System.out.format("\nThe First Character Occurrence of %c at %d position", del_ch, i);
		
		sb.deleteCharAt(i);		
		System.out.println("\nThe Final String after Deleting " + del_ch + " = " + sb);
		
		sbuff.deleteCharAt(i);
		System.out.println("\nThe Final String after Deleting First Occurrence " 
		+ del_ch + " = " + sbuff);
	}
}
Please Enter String to Delete First Character =  delete

Enter the Character to Delete =  e

The First Character Occurrence of e at 1 position
The Final String after Deleting e = dlete

The Final String after Deleting First Occurrence e = dlete

To remove the first character occurrence, we can use the Java String substring function. In this Java program, delFirstCharStr.indexOf(del_ch) finds the index position of first character occurrence. Next, delFirstCharStr.substring(0, i) returns substring up to i. Then, delFirstCharStr.substring(i + 1) to concat string from next index position to end of the delFirstCharStr string.

import java.util.Scanner;

public class DeleteFirstCharOcc4 {
	private static Scanner sc;
	public static void main(String[] args) {
		String delFirstCharStr;
		char del_ch;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to Delete Character =  ");
		delFirstCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Delete =  ");
		del_ch = sc.next().charAt(0);
		
		int i = delFirstCharStr.indexOf(del_ch);
		String out = delFirstCharStr.substring(0, i) + delFirstCharStr.substring(i + 1);
		
		System.out.println("\nThe Final String after Deleting " + del_ch + " = " + out);
	}
}
Please Enter String to Delete Character =  hi coding

Enter the Character to Delete =  i

The Final String after Deleting i = h coding

In Java, the string replaceFirst function replaces the first occurrence of a charter with the new character. Here, delFirstCharStr.replaceFirst(String.valueOf(del_ch), “”) removes first occurrence of del_ch with empty string. Remember, this function accepts string arguments, so that we converted that char to string.

import java.util.Scanner;

public class DeleteFirstCharOcc5 {
	private static Scanner sc;
	public static void main(String[] args) {
		String delFirstCharStr, x = "";
		char del_ch;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to Delete Character =  ");
		delFirstCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Delete =  ");
		del_ch = sc.next().charAt(0);
		
		x = delFirstCharStr.replaceFirst(String.valueOf(del_ch), "");

		System.out.println("\nThe Final String after Deleting " + del_ch + " = " + x);
	}
}
Please Enter String to Delete Character =  remove char

Enter the Character to Delete =  e

The Final String after Deleting e = rmove char

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

import java.util.Scanner;

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

		System.out.print("\nPlease Enter String to Delete First Character =  ");
		delFirstCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Delete =  ");
		del_ch = sc.next().charAt(0);
		
		char[] delFirstCharArr = delFirstCharStr.toCharArray();
		
		for(i = 0; i < delFirstCharArr.length; i++) {
			if(delFirstCharStr.charAt(i) == del_ch) {
				delFirstCharArr[i] = 0;
				break;
			}
		}
		System.out.print("\nThe Final String after Deleting " + del_ch + " = " );
		System.out.print(delFirstCharArr);
		
	}
}
Please Enter String to Delete First Character =  programming

Enter the Character to Delete =  g

The Final String after Deleting g = proramming

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. Last Character Occurrence in a String
  6. Replace Last Character Occurrence in a String
  7. Remove the 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