- 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
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
- Related Articles
- Finding the index of a character in a Swift string
- Find last index of a character in a string in C++
- Return index of first repeating character in a string - JavaScript
- Search for a character from a given position in Java
- Finding the index of the first repeating character in a string in JavaScript
- How can I search a character or substring in java?
- Return the index of first character that appears twice in a string in JavaScript
- Finding the last occurrence of a character in a String in Java
- Java Program to locate a character in a string
- Python program to change character of a string using given index
- Java Program to get a character located at the String's specified index
- Find the index of the first unique character in a given string using C++
- How to search for a pattern in a Java string?
- C# program to replace n-th character from a given index in a string
- Left pad a String with a specified character in Java

Advertisements