
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find 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
Advertisements