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 1

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

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.