🎓 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
StringBuffer.lastIndexOf() method in Java is used to find the index of the last occurrence of a specified substring within the StringBuffer object. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
lastIndexOfMethod Syntax- Examples
- Finding the Last Occurrence of a Substring
- Finding the Last Occurrence of a Substring from a Specified Index
- Conclusion
Introduction
The lastIndexOf() method is a member of the StringBuffer class in Java. It allows you to search for the last occurrence of a substring within the StringBuffer and returns the index of that occurrence. If the substring is not found, the method returns -1.
lastIndexOf Method Syntax
The syntax for the lastIndexOf method is as follows:
public synchronized int lastIndexOf(String str)
Parameters:
str- the substring to search for.
Returns:
- The index of the last occurrence of the specified substring, or -1 if there is no such occurrence.
public synchronized int lastIndexOf(String str, int fromIndex)
Parameters:
str- the substring to search for.fromIndex- the index to start the search backward from.
Returns:
- The index of the last occurrence of the specified substring, searching backward starting at the specified index, or -1 if there is no such occurrence.
Examples
Finding the Last Occurrence of a Substring
The lastIndexOf method can be used to find the index of the last occurrence of a specified substring within the StringBuffer object.
Example
public class StringBufferLastIndexOfExample {
public static void main(String[] args) {
// Create a StringBuffer object with initial content
StringBuffer sb = new StringBuffer("Hello, World! Hello, Universe!");
// Find the index of the last occurrence of "Hello"
int index = sb.lastIndexOf("Hello");
// Print the index
System.out.println("Last index of 'Hello': " + index);
}
}
Output:
Last index of 'Hello': 14
Finding the Last Occurrence of a Substring from a Specified Index
The lastIndexOf method can also be used to find the index of the last occurrence of a specified substring, starting the search backward from a given index.
Example
public class StringBufferLastIndexOfExample {
public static void main(String[] args) {
// Create a StringBuffer object with initial content
StringBuffer sb = new StringBuffer("Hello, World! Hello, Universe!");
// Find the index of the last occurrence of "Hello" starting from index 12
int index = sb.lastIndexOf("Hello", 12);
// Print the index
System.out.println("Last index of 'Hello' from index 12: " + index);
}
}
Output:
Last index of 'Hello' from index 12: 0
Conclusion
The StringBuffer.lastIndexOf() method in Java provides a way to find the index of the last occurrence of a specified substring within a StringBuffer object. By understanding how to use this method, you can efficiently search for substrings from the end of the StringBuffer and manage text data. This method is particularly useful for applications that require searching and locating specific patterns or substrings within larger text sequences.
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