Java Program to Print First and Last Character in a String

Write a Java Program to print or display First and Last Character in a String with an example. In this java example, we used the String charAt function and the index position of first and last character to return the characters. To get the last character index position, we used the string length function.

import java.util.Scanner;

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

		System.out.print("\nEnter String to Print 1st & last Character =  ");
		PrintFirstLastChar = sc.nextLine();		
		
		char firstCh = PrintFirstLastChar.charAt(0);		
		System.out.println("\nThe First Character in " + PrintFirstLastChar + " = " +  firstCh);
		
		char lastCh = PrintFirstLastChar.charAt(PrintFirstLastChar.length() - 1);		
		System.out.println("The Last Character in " + PrintFirstLastChar + "  = " +  lastCh);
	}
}
Java Program to Print First and Last Character in a String 1

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

The Java StringBuilder has the charAt function that returns the character at the index position. So, we used this Java function to get the first and last character of a string.

import java.util.Scanner;

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

		System.out.print("\nEnter String to Print 1st & last Character =  ");
		PrintFirstLastChar = sc.nextLine();		
		
		StringBuilder sb = new StringBuilder(PrintFirstLastChar);
		
		char firstCh = sb.charAt(0);		
		System.out.println("\nThe First Character in " + PrintFirstLastChar + " = " +  firstCh);
		
		char lastCh = sb.charAt(PrintFirstLastChar.length() - 1);		
		System.out.println("The Last Character in " + PrintFirstLastChar + "  = " +  lastCh);
	}
}

Java First and Last String Character output


Enter String to Print 1st & last Character =  Java

The First Character in Java = J
The Last Character in Java  = a

Java Program to return First and Last Character in a String using the StringBuffer. As we all know, StringBuffer also has the charAt function, and we use the same.

import java.util.Scanner;

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

		System.out.print("\nEnter String to Print 1st & last Character =  ");
		PrintFirstLastChar = sc.nextLine();		
		
		StringBuffer sbuff = new StringBuffer(PrintFirstLastChar);
		
		char firstCh = sbuff.charAt(0);		
		System.out.println("\nThe First Character in " + PrintFirstLastChar + " = " +  firstCh);
		
		char lastCh = sbuff.charAt(PrintFirstLastChar.length() - 1);		
		System.out.println("The Last Character in " + PrintFirstLastChar + "  = " +  lastCh);
		
	}
}
Enter String to Print 1st & last Character =  Tutorial Gateway

The First Character in Tutorial Gateway = T
The Last Character in Tutorial Gateway  = y

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

import java.util.Scanner;

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

		System.out.print("\nEnter String to Delete 1st & last Character =  ");
		PrintFirstLastChar = sc.nextLine();		
		
		StringBuilder sb = new StringBuilder(PrintFirstLastChar);
		StringBuffer sbuff = new StringBuffer(PrintFirstLastChar);
		
		System.out.println("\nString Builder - String First Character  = " +  sb.charAt(0));		
				
		System.out.println("String Buffer  - String First Character  = " +  sbuff.charAt(0));
			
		System.out.println("\nString Builder - String Last Character  = " +  sb.charAt(sb.length() - 1));
		
		System.out.println("String Buffer  - String Last Character  = " +  sbuff.charAt(sbuff.length() - 1)); 
	}
}
Enter String to Delete 1st & last Character =  Java Programming

String Builder - String First Character  = J
String Buffer  - String First Character  = J

String Builder - String Last Character  = g
String Buffer  - String Last Character  = g