Chryssa Aliferi

About Chryssa Aliferi

Chryssa is a Computer Science graduate from Athens University of Economic and Business. During her studies, Chryssa carried out a great variety of projects ranging from networking to software engineering. She is very keen on front end development especially on mobile technologies and web applications.

Java String indexOf Example

It is often important to search for a character or a character set in a Java String. For example, in the parsing of Java Strings, we might want to provide the possibility for a search through strings, in order to find a specific character or word. Thus we need a “tool”, a method that will help us make it.

So, in this example we are going to talk about identifying characters and subsequences of characters through Java Strings, with the method indexOf().

String static indexOf() method:

The indexOf()(String or character target) method searches left-to-right inside the given string for a “target” string. The method returns the index number where the target string is first found or -1 if the target is not found. Like equals(), the indexOf() method is case-sensitive, so uppercase and lowercase chars are considered to be different.

  • int indexOf(String str): Returns the index within this string of the first occurrence of the specified substring. If it does not occur as a substring, -1 is returned.
  • int indexOf(String str, int fromIndex): Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. If it does not occur, -1 is returned.

We can use the same methods in order to find the index of the first occurrence of a specified character within the string:

  • int indexOf(int ch)
  • int indexOf(int ch, int fromIndex)

Example:

JavaStringIndexOf.java

package com.javacodegeeks.javabasics.string;

public class JavaStringIndexOf {

   public static void main(String args[]) {

   String str = new String("Hello JavaCodeGeeks! This is my very first post.");

   String subStr1 = new String("JavaCodeGeeks");
   String subStr2 = new String("JavaGeeks");

   System.out.print("Found Index :");
   System.out.println(str.indexOf('e')); //finds the first occurence of 'e'

   System.out.print("Found Index :");
   System.out.println(str.indexOf('e', 5)); //finds the occurence of 'e' after the 5th character

   System.out.print("Found Index :");
   System.out.println(str.indexOf(subStr1)); //finds the first occurence of "JavaCodeGeeks"

   System.out.print("Found Index :");
   System.out.println(str.indexOf(subStr1, 15)); //finds the occurence of "JavaCodeGeeks" after the 15th character

   System.out.print("Found Index :");
   System.out.println(str.indexOf(subStr2)); //finds the first occurence of "JavaGeeks"

   }
}

Output:


Found Index :1
Found Index :13
Found Index :6
Found Index :-1
Found Index :-1

This was the example of Java String indexOf() method.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close