How to Use Java String Class indexOf() Method

Java String Class indexOf() Method

Function Description

Java String class indexOf() method is used to get the index value of the first occurrence of the specified character or the sequence of characters within the string. Search starts from the specified index, if fromIndex is given. If fromIndex value is not given, the search starts from 0 index.

It returns -1, if it does not find the specified character or sequence of characters within the String.

If the fromIndex value is negative(-ve), it will have the same effect as index value 0 and the entire string will be searched.

If the fromIndex value is greater than the length of the string, it will have the same effect as the length of the string and will return -1.

In java indexOf() method is defined as overloaded method

  1. Method to get the index value of the first occurrence of the specified character within the string.

  2. Method to get the index value of the first occurrence of the specified character within the string. The specified character search starts from the specified index.

  3. Method to get the index value of the first occurrence of the specified string within the string.

  4. Method to get the index value of the first occurrence of the specified sequence of characters(string) within the string. The specified string search starts from the specified index.

Method Signature

Method to get the index value of the first occurrence of the specified character within the string. In this method, the search starts from 0 index.

public int indexOf(int ch)

Method to get the index value of the first occurrence of the specified character within the string. Specified character search starts from the specified fromIndex. 

public int indexOf(int ch, int fromIndex)

Method to get the index value of the first occurrence of the specified string within the string

public int indexOf(String str)

Method to get the index value of the first occurrence of the specified sequence of characters(string) within the string. The specified string search starts from the specified index.

public int indexOf(String str, int fromIndex)

Parameter(s)

ch : Character for which first occurrence of index value to be searched.

fromIndex: Starting index value from where character/sequence of characters will be searched.

str: Sequence of characters(string) for which the first occurrence of index value to be searched.

Return

Index value of the first occurrence of character/sequence of characters within the string/substring in int datatype. It returns -1, if specified character/sequence of characters does not find within the string

Examples of Java String Class indexOf() Method

Example to get the index value of the first occurrence of the specified character within the string

public class ExampleStringIndexOf {
 
    private void getIndexOfCharacter() {

          String str = new String("Welcome to String class indexOf() function learning");

          // returns the index value S character

          int indexValue = str.indexOf('S');

          System.out.println("Index value of S character : " + indexValue);
 
          // returns the index value f character

          indexValue = str.indexOf('f');

          System.out.println("Index value of f character : " + indexValue);
    }
 
   
    public static void main(String[] args) {

          ExampleStringIndexOf example = new ExampleStringIndexOf();

          example.getIndexOfCharacter();
    }
 
}


Output:

Index value of S character : 11

Index value of f character : 30

Example to get the index value of the first occurrence of the specified character within the string for which fromIndex value is given to start the search at.

public class ExampleStringIndexOf {
 
    private void getIndexOfCharacterFromSpecifiedIndex() {

          String str = new String("Welcome to String class indexOf() function learning");

          int indexOfS = str.indexOf('S',10);

          System.out.println("Index value of S character while search starts from index value 10 is " + indexOfS);
 
          indexOfS = str.indexOf('W', 5);

          System.out.println("Index value of W character while search starts from index value 5 is" + indexOfS);

    }
 
    public static void main(String[] args) {

          ExampleStringIndexOf example = new ExampleStringIndexOf();

          example.getIndexOfCharacterFromSpecifiedIndex();
    }
 
}

Output:

Index value of S character while search starts from index value 10 is 11

Index value of W character while search starts from index value 5 is-1

Example to get the index value of the first occurrence of the specified string within the String

public class ExampleStringIndexOf {
 
    private void getIndexOfString() {

          String str = new String("Welcome to String indexOf function learning");

          // returns index value of 'function' string

          int indexValue = str.indexOf("function");

          System.out.println("Index value of 'function' = " + indexValue);
         
          System.out.println("Index value of 'Welcome' = " + str.indexOf("Welcome"));
         
          System.out.println("Index value of 'test' = " + str.indexOf("test"));

    }
  
   
    public static void main(String[] args) {
 
         ExampleStringIndexOf example = new ExampleStringIndexOf();

          example.getIndexOfString();
    }
 
}

Output:

Index value of 'function' = 26

Index value of 'Welcome' = 0

Index value of 'test' = -1

Example to get the index value of the first occurrence of the specified sequence of characters(string) within the string while fromIndex value is given to start the search at.

public class ExampleStringIndexOf {
 
    private void getIndexOfStringFromSpecifiedIndex() {

          String str = new String("Welcome to String indexOf function learning");

          // returns index value of 'function', searching from index value 5

          int index = str.indexOf("function", 5);

          System.out.println("Index value of 'function', searching from index value 5 is" + index);
 
          // returns index value of 'Welcome',searching from index value 5

          index = str.indexOf("Welcome", 5);

          System.out.println("Index value of 'Welcome', searching from index value 5 is " + index);
 
    }
 
    public static void main(String[] args) {

          ExampleStringIndexOf example = new ExampleStringIndexOf();

          example.getIndexOfStringFromSpecifiedIndex();
    }
 
}
 

Output:

Index value of 'function', searching from index value 5 is 26

Index value of 'Welcome', searching from index value 5 is -1

Other Methods of String Class

SequenceMethodDescription
1char charAt(int index)This method is used to return the character of the string at specified index. First character is at index 0 while last is at string length-1 . StringIndexOutOfBounds Exception is thrown if valid index value is not passed.
2int length()Returns the number of characters count  in  a string.
3static String format(String format, Object… args)Return the formatted String. It is a static method and formats the string as per passing format and argument(s).
4static String format(Locale l, String format, Object… args)Return the formatted String. It is a static method and formats the string as per passing format and argument(s) and locale.
5String substring(int beginIndex)Returns substring from the given index position to the end index of the string.
6String substring(int beginIndex, int endIndex)Returns substring from the specified begin index to the specified end index.
7boolean contains(CharSequence s)Returns boolean value(true/false). It searches the given sequence of characters within the string and returns true/false accordingly. Characters will be case sensitive while searching in string.
8static String join(CharSequence delimiter, CharSequence… elements)Returns joined string. This method joins each passing String using the specified delimiter. It is a static method.
9static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)Returns the joined string. This method joins each passing string object of an iterate object using the specified delimiter and returns the concatenated string. It is a static method.
10boolean equals(Object another)Returns boolean value(true/false). It checks the equality of string with the passing object.
11boolean isEmpty()Returns boolean value(true/false). It returns true if the string is empty otherwise returns false.
12String concat(String str)Returns the concatenating string. It concatenates the passing string at the end of the string.
13String replace(char old, char new)Return the replaced string. It replaces all occurrences of the specified character with the new character.
14String replace(CharSequence old, CharSequence new)Return the replaced string. It replaces all occurrences of the specified Character sequence with the specified new Character sequence.
15String replaceAll(String regex, String replacement)Returns the replaced string. It replaces all occurrences of the sequence of characters with the given replacement string that are matching with the given regular expression.
16String replaceFirst(String regex, String replacement)Returns the replaced string. It replaces the first matching sequence of characters with the replacement string that matches with the given regular expression.
17static boolean equalsIgnoreCase(String another)Returns the boolean (true/false) value. It compares the passing string with string by ignoring the case. It  compares the content irrespective of lowercase or uppercase of the character(s).
18String[] split(String regex)Returns the array of strings resulting from splitting of the string     around matches of the given regular expression. String is breaked into strings from all  the matching regular expressions.
19String[] split(String regex, int limit)Returns the array of strings resulting from splitting of the string     around matches of the given regular expression. It breaks the string from all the matching regular expression upto the given limit.
20String intern()Returns an interned string. This method helps to compare two objects using == operator and checks in a string constant pool. it is faster for comparison.
21int lastIndexOf(int ch)Returns the index value of last occurrence of the specified character
22int lastIndexOf (int ch, int fromIndex)Returns the index value of last occurrence of the specified character, search starts at given fromIndex. 
23int lastIndexOf (String str)Returns the index value of last occurrence of the specified string.
24int lastIndexOf (String str, int fromIndexReturns the index value of last occurrence of the specified string, searches start from given fromIndex
25String toLowerCase()Returns all the characters of the string in lowercase .
26String toLowerCase(Locale l)Returns all the characters of the string in lowercase using specified locale.
27String toUpperCase()Returns all the characters of the string in uppercase.
28String toUpperCase(Locale l)Returns all the characters of the string in uppercase using specified locale.
29String trim()Returns the string after removing the white space from the beginning and end of the string.
30static String valueOf(int value)Return the converted string. It converts the objects and all primitive types of values into string. It is a static method.

In this article, we learned all about the indexOf() method of Java String class with examples. Apart from the IndexOf() method, we got an idea about other String methods too. We will come up with more articles explaining other String methods with examples. For more details please click on the respective methods link.

3 thoughts on “How to Use Java String Class indexOf() Method

  1. Wow! Finally I got a website from where I can truly get helpful facts regarding my study and knowledge. Jennica Dmitri Blessington

  2. It is not my first time to pay a quick visit this site, i am browsing this web page dailly and take good facts from here everyday. Tamara Murry Shaddock

Leave a Reply

Your email address will not be published. Required fields are marked *