- 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
Search index of a character in a string in Java
Use the indexOf() method to search the index of a character in a string.
Let’s say the following is our string.
String myStr = "amit";
Now, let us find the index of character ‘t’ in the above string.
strIndex = myStr.indexOf('t');
The following is the final example.
Example
public class Demo { public static void main(String[] args) { String myStr = "amit"; int strIndex = 0; System.out.println("String: "+myStr); // finding index of character t strIndex = myStr.indexOf('t'); System.out.println("Character m found at index: "+strIndex); } }
Output
String: amit Character m found at index: 3
Advertisements