🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
substring method, the StringBuilder class, and the Stream API (Java 8 and later).Table of Contents
- Introduction
- Using
substringMethod - Using
StringBuilderClass - Using
StreamAPI - Conclusion
Introduction
In Java, strings are sequences of characters. Removing the last character from a string can be done using various methods, each suited to different scenarios.
Using substring Method
The substring method of the String class can be used to remove the last character by creating a new string that excludes the last character.
Example
public class RemoveLastCharacterExample {
public static void main(String[] args) {
String str = "Hello World!";
String result = removeLastCharacter(str);
System.out.println("Original String: \"" + str + "\"");
System.out.println("String without last character: \"" + result + "\"");
}
public static String removeLastCharacter(String str) {
if (str == null || str.length() == 0) {
return str;
}
return str.substring(0, str.length() - 1);
}
}
Explanation
- The
removeLastCharactermethod checks if the string isnullor empty. - The
substringmethod is called on the stringstrto create a new string that includes all characters except the last one.
Output:
Original String: "Hello World!"
String without last character: "Hello World"
Using StringBuilder Class
The StringBuilder class can be used to remove the last character by manipulating the character sequence.
Example
public class RemoveLastCharacterExample {
public static void main(String[] args) {
String str = "Hello World!";
String result = removeLastCharacter(str);
System.out.println("Original String: \"" + str + "\"");
System.out.println("String without last character: \"" + result + "\"");
}
public static String removeLastCharacter(String str) {
if (str == null || str.length() == 0) {
return str;
}
StringBuilder sb = new StringBuilder(str);
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
}
Explanation
- The
removeLastCharactermethod checks if the string isnullor empty. - A
StringBuilderobject is created with the stringstr. - The
deleteCharAtmethod is called on theStringBuilderobject to remove the last character. - The
StringBuilderis converted back to a string.
Output:
Original String: "Hello World!"
String without last character: "Hello World"
Using Stream API
The Stream API (introduced in Java 8) can also be used to remove the last character from a string.
Example
import java.util.stream.Collectors;
public class RemoveLastCharacterExample {
public static void main(String[] args) {
String str = "Hello World!";
String result = removeLastCharacter(str);
System.out.println("Original String: \"" + str + "\"");
System.out.println("String without last character: \"" + result + "\"");
}
public static String removeLastCharacter(String str) {
if (str == null || str.length() == 0) {
return str;
}
return str.chars()
.limit(str.length() - 1)
.mapToObj(c -> String.valueOf((char) c))
.collect(Collectors.joining());
}
}
Explanation
- The
removeLastCharactermethod checks if the string isnullor empty. - A stream is created from the characters of the string using
str.chars(). - The
limitmethod is used to limit the stream to all characters except the last one. - The
mapToObjmethod converts the characters back to strings. - The
collectmethod joins the characters back into a single string without the last character.
Output:
Original String: "Hello World!"
String without last character: "Hello World"
Conclusion
Removing the last character from a string in Java can be accomplished using various methods, each with its own advantages. The substring method provides a simple and direct way to create a new string without the last character. The StringBuilder class offers a flexible approach for manipulating character sequences. The Stream API provides a modern and functional programming approach, making the code more readable and expressive. Depending on your specific use case and preferences, you can choose the method that best fits your needs.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment