Java Program to Remove First and Last Character in a String

Write a Java Program to remove or delete First and the Last Character in a String with an example. In this java Remove First and Last Character example, we used the StringBuilder deleteCharAt function. To get the last index position, we used the string length function on the delFirstLastCharStr.

The Java StringBuilder has the deleteCharAt function that deletes the character at the given index position. So, we used this deleteCharAt function to remove the first and last character of a delFirstLastCharStr string.

import java.util.Scanner;

public class DeleteFirstLastCharOcc1 {
	private static Scanner sc;
	public static void main(String[] args) {
		String delFirstLastCharStr;
		
		sc= new Scanner(System.in);

		System.out.print("\nEnter String to Delete 1st & last Character =  ");
		delFirstLastCharStr = sc.nextLine();		
		
		StringBuilder sb = new StringBuilder(delFirstLastCharStr);
		sb.deleteCharAt(0);		
		System.out.println("\nThe Final String after Deleting First Char  = " +  sb);
		
		sb.deleteCharAt(sb.length() - 1);		
		System.out.println("\nThe Final String after Deleting First & Last Char  = " +  sb);
	}
}

Java remove first and last string character output

Enter String to Delete 1st & last Character =  hello

The Final String after Deleting First Char  = ello

The Final String after Deleting First & Last Char  = ell

Java Program to Remove First and Last Character in a String Example 2

The Java StringBuffer also has the deleteCharAt function that removes the character at the index position. So, we used this function on delFirstLastCharStr to remove the first and last character of a string.

import java.util.Scanner;

public class DeleteFirstLastCharOcc2 {
	private static Scanner sc;
	public static void main(String[] args) {
		String delFirstLastCharStr;
		
		sc= new Scanner(System.in);

		System.out.print("\nEnter String to Delete 1st & last Character =  ");
		delFirstLastCharStr = sc.nextLine();		
		
		StringBuffer sbuff = new StringBuffer(delFirstLastCharStr);
		sbuff.deleteCharAt(0);		
		System.out.println("\nThe Final String after Deleting First Char  = " +  sbuff);
		
		sbuff.deleteCharAt(sbuff.length() - 1);		
		System.out.println("\nThe Final String after Deleting First & Last Char  = " +  sbuff);
	}
}
Enter String to Delete 1st & last Character =  java

The Final String after Deleting First Char  = ava

The Final String after Deleting First & Last Char  = av

It is another Java example to delete the first and the last string character.

import java.util.Scanner;

public class DeleteFirstLastCharOcc3 {
	private static Scanner sc;
	public static void main(String[] args) {
		String delFirstLastCharStr;
		
		sc= new Scanner(System.in);

		System.out.print("\nEnter String to Delete 1st & last Character =  ");
		delFirstLastCharStr = sc.nextLine();		
		
		StringBuilder sb = new StringBuilder(delFirstLastCharStr);
		StringBuffer sbuff = new StringBuffer(delFirstLastCharStr);
		
		sb.deleteCharAt(0);		
		System.out.println("\nString Builder - String after Deleting First Char  = " +  sb);		
		
		sbuff.deleteCharAt(0);		
		System.out.println("String Buffer  - String after Deleting First Char  = " +  sbuff);
		
		sb.deleteCharAt(sb.length() - 1);		
		System.out.println("\nString Builder - String after Deleting First and Last Char  = " +  sb);
		
		sbuff.deleteCharAt(sbuff.length() - 1);		
		System.out.println("String Buffer  - String after Deleting First and Last Char  = " +  sbuff);
	}
}
Enter String to Delete 1st & last Character =  hello world

String Builder - String after Deleting First Char  = ello world
String Buffer  - String after Deleting First Char  = ello world

String Builder - String after Deleting First and Last Char  = ello worl
String Buffer  - String after Deleting First and Last Char  = ello worl

This Java delete first and the last Character example uses the substring function. Here, delFirstLastCharStr.substring(1, delFirstLastCharStr.length() – 1) returns string starts at index position 1 to length-1

import java.util.Scanner;

public class DeleteFirstLastCharOcc4 {
	private static Scanner sc;
	public static void main(String[] args) {
		String delFirstLastCharStr;
		
		sc= new Scanner(System.in);

		System.out.print("\nEnter String to Delete 1st & last Character =  ");
		delFirstLastCharStr = sc.nextLine();		
		
		String out = delFirstLastCharStr.substring(1, delFirstLastCharStr.length() - 1);
	
		System.out.println("\nThe Final String after Deleting First and Last Char  = " +  out);
	}
}
Java Program to Remove First and Last String character 4