Remove the Last Character from String in Java

In this tutorial, we will learn to remove the last character from String in Java in different ways. Java Class Library(JCL) contains a class called String which is available under java.lang package. It allows the user to create an object or declare a variable to store a set of characters.

Like Math and character classes, String classes also possess some useful functions to operate or manipulate string data. Here, we will see how to remove the last character from String in java. There are different ways in which we can remove the last character. They are:

  1. String substring()
  2. StringBuffer.deleteCharAt():
  3. ReplaceAll():
  4. StringBuffer.delete():

You can also check: How to remove a particular character from a string in Java?

Ways to remove the last Character from String in Java

1) String substring():

We can remove the last character from the string using the substring() method. It will return a set of characters simultaneously from the starting index up to the last index of the string. (excluding the character present at the last index).

Syntax :

<String variable> = <String.substring(int start_index, int last_index)>;

For example:

String s = "COMPUTER";
String p = s.substring(0, s.length()-1);

CODE:

import java.util.*;
public class Main{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int n;
String st;
System.out.println("Enter the string :");
st = in.next();
n = st.length();
System.out.println("The new string is : ");
System.out.println(st.substring(0,n-1));
}
}

OUTPUT:

Enter the string :

Computer

The new string is:

Compute

2) StringBuffer.deleteCharAt():

StringBuffer is another type of class with some added functionality to handle strings. We can declare an object of StringBuffer type. This creates a reasonable space in the memory called Buffer. In StringBuffer, we have StringBuffer.deleteCharAt() function. Using this function we can remove the last character of the string by providing the index number in the parameter of the method.

Syntax:

<StringBuffer variable>.deleteCharAt(<index>);

In this way,

String st = "COMPUTER";
StringBuffer s = new StringBuffer(st);
s.deleteCharAt(st.length()-1);

CODE:

import java.lang.*;
import java.util.*;

public class Main {

public static void main(String[] args)
{
Scanner in =new Scanner(System.in);
String st;
System.out.println("Enter the string: ");
st = in.next();
StringBuffer s = new StringBuffer(st);
System.out.println("The new String using StringBuffer: ");
s.deleteCharAt(st.length()-1);
System.out.println(s);
}
}

OUTPUT:

Enter the string:

Computer

The new string using StringBuffer:

Compute

3) ReplaceAll():

This is just a regular way to remove or delete a character from the string. It parses two parameters one in which we have to check to which character does it match and the other one is the character to be replaced with the matched character.

Syntax:

<String variable>.replaceAll<(regex,replacement)>;

For example:

String string="COMPUTER";
string=string.replaceAll(".$", "");

CODE:

import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);

String str;
System.out.println("Enter the string: ");
str = in.next();
System.out.println("The new string after replacing with a space: ");
str=str.replaceAll(".$", "");
System.out.println(str);
}
}

OUTPUT:

Enter the string:

Computer

The new string after replacing with a space:

Compute

4) StringBuffer.delete():

We can delete the last character from the string using the delete() method provided in the StringBuffer class. This deletes the characters from the specified first index till the specified second index in the given string.

Syntax:

<StringBuffer variable>.delete<(index1,index2)>;

In this way:

String str = "Computer";
StringBuffer sb = new StringBuffer(str);
sb.delete(str.length() - 1, str.length());

CODE:

import java.util.*;
class Main {

public static void main(String args[])
{
Scanner in = new Scanner(System.in);
String str;
System.out.println("Enter the string");
str=in.next();
StringBuffer sb = new StringBuffer(str);
System.out.println("The new string after deleting: ");
sb.delete(str.length() - 1, str.length());
System.out.println(sb);
}
}

OUTPUT:

Enter the string

Computer

The new string after deleting:

Compute

Hope this tutorial was useful.

Leave a Reply

Your email address will not be published.