- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding the last occurrence of a character in a String in Java
Use the lastIndexOf() method to find the last occurrence of a character in a string in Java.
Let’s say the following is our string.
String myStr = "Amit Diwan";
In the above string, we will find the last occurrence of character ‘i’
myStr.lastIndexOf('i');
The following is the complete example.
Example
public class Demo { public static void main(String[] args) { String myStr = "Amit Diwan"; int strLastIndex = 0; System.out.println("String: "+myStr); strLastIndex = myStr.lastIndexOf('i'); System.out.println("The last index of character a in the string: "+strLastIndex); } }
Output
String: Amit Diwan The last index of character a in the string: 6
- Related Articles
- Replace first occurrence of a character in Java
- Java program to count the occurrence of each character in a string using Hashmap
- How to get everything before the last occurrence of a character in MySQL?
- Get the last occurrence of a substring within a string in Arduino
- Finding the index of a character in a Swift string
- Java program to check occurrence of each character in String
- How to remove the last character from a string in Java?
- String function to replace nth occurrence of a character in a string JavaScript
- C program to replace all occurrence of a character in a string
- C Program to find minimum occurrence of character in a string
- C Program to find maximum occurrence of character in a string
- Get the substring before the last occurrence of a separator in Java
- C# Program to find number of occurrence of a character in a String
- How to replace the last occurrence of an expression in a string in Python?
- Find last index of a character in a string in C++

Advertisements