In java, to remove last character from a string we can use substring() method or wa can also achieve this by creating a custom method.
For the given input string “Hello”, after removing the last character from the input string, resulting output will be “Hell”.
In this article we will learn to remove last letter in a string through an example, detailed logic and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java operators
- String methods
- Conditional statements
Problem Statement
We need to write a java program to remove last character from a string. The program should take an input string and it should remove the last character from the string and print the remaining string. We will achieve this in our program. Let’s see an example,
Example:
For a given input String str “Interview Expert”, Output should be “Interview Exper”.
Explanation:
Input String str: "Interview Expert"
After removing last character from str, updated str will be:
str = "Interview Exper"
Hence, "Interview Exper" is the result for the input str "Interview Expert".
Logic to remove last character from a given string
There are multiple logics to remove the last character from a given string in the java programming language.
Logic 1: Using substring() method
- To remove last character in a string using substring() method, we will start our program by initializing the string str from user.
- Trim the str using trim() method to remove trailing and leading spaces.
- Using if-else, It checks if the input string is not null and its length is greater than 1 to ensure that there is at least one character to remove.
- If str meets this condition, it uses the substring method to extract a substring from index 0 to the second-to-last character, effectively removing the last character
- if str is null or has only one character, it assigns an empty string to the variable str and print the output.
Logic 2: Custom method
- To remove last character in a string by creating a custom method, we need to create a custom method which takes string type data (str) as input.
- Using if else statements, check if the input str is not null to prevent a NullPointerException.
- If the str is not null, ensure that its length is greater than 1 to ensure there is at least one character to remove.
- Convert the input text to a character array using the toCharArray() method.
- Create a new character array with a length one less than the original array.
- Copy all characters from the original array to the new array, except the last character.
- Convert the new character array to a string using new String(). If the str is null or its length is less than 1, return an empty string.
- Call this method in the main method and print the result.
š§® Java program to remove last character from a given string
ā Ā Program 1: Using substring() method
In the below program we are using substring() method to remove last character from a given string.
import java.util.Scanner;
public class LastCharRemove {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter a text");
String str = s.nextLine();
str = str.trim();
if (str != null && str.length() > 1) {
str = str.substring(0, str.length()-1);
} else {
str = "";
}
System.out.println("Last character is removed: " + str);
s.close();
}
}
Output:
Enter a text
Java
Last character is removed: Jav
š§ Explanation:
- The java program imports the Scanner class from java.util package.
- Define a public class named LastCharRemove.
- Define the main method inside the LastCharRemove class.
- Create a Scanner object ‘s’ to read input from the user.
- Ask the user to enter text.
- Read the input text entered by the user using the nextLine() method of Scanner class and store it in the ‘str’ variable.
- Trim the input str to remove any leading or trailing whitespaces.
- Check if the input str is not null and has a length greater than 1.
- If the input str meets the conditions, use the substring() method to remove the last character from the text.
- If the input str is null or has only one character, assign an empty string to the ‘str’ variable.
- Print the modified str (with the last character removed).
- Close the Scanner object ‘s’ to release system resources.
ā Ā Program 2: Custom method
In the below program we are removing last character from a given string by creating a custom method.
import java.util.Scanner;
public class LastCharRemove2 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter a text");
String str = s.nextLine();
str = str.trim();
String output= removeLastChar(str);
System.out.println("Last removed character is: " +output);
s.close();
}
public static String removeLastChar(String str) {
if (str != null && str.length() > 1) {
char ch[] = str.toCharArray();
char newChar[]= new char[ch.length-1];
System.arraycopy(ch, 0, newChar, 0, newChar.length);
return new String(newChar);
}
else {
return "" ;
}
}
}
Output:
Enter a text
Learn Coding
Last removed character is: Learn Codin
š§ Explanation:
- It imports the
Scanner
class to read user input. - In the
main
method, it asks the user to enter a text, reads the input, and trims any leading or trailing whitespace from the input string. - It then calls the
removeLastChar
method, passing the input string as an argument. - In the
removeLastChar
method, it checks if the input string is not null and its length is greater than 1 to ensure that there is at least one character to remove. - If the conditions are met, it converts the input string to a character array and creates a new character array with one less element.
- It copies all elements from the original character array to the new character array, except for the last character.
- It then returns the new string created from the modified character array.
- If the input string is null or has only one character, it returns an empty string.
- Finally, in the
main
method, it prints the modified string, which is the original string with the last character removed.
Conclusion
The java programs we did here will remove last character from a given string using substring() method and by creating a custom method.