Guide To Check Char Is In String In Java - indexOf(), lastIndexOf​()

1. Overview

In this tutorial, We will check the possible ways to check the given char or string in the given string. If the char is present then need to return it's index.

Throughout this post, we will be writing the examples on below string.

Check Char Is In String

String string = "find string";


indexof() and lastIndexOf methods return int value as index of the found character.

Please go through the previous post, how String contains() method works in java with examples. This post will give you understanding how indexof method is used internally.

2. indexof()

2.1 indexOf​(int ch)


indexof method is present in java.lang.String package which takes char as method argument and returns int which is the index of the matched chat if found.

Simple words, This method returns the index within this string of the first occurrence of the specified character.

int index = string.indexOf('d');
System.out.println("index for chat d: " + index);

Output:

index for char d: 3

Even though if char 'd' appears multiple time also, it returns first match.

Internally, indexOf method runs the for loop from string index 1 to its length and check every character with the given character by invoking charAt() method.

this.charAt(k) == ch

where k is index from for loop.

If it finds the given char then return its index. If not, returns -1.

int index = string.indexOf('z');

Output:

index for char z: -1

2.2 indexOf​(int ch, int fromIndex)


This method works similar to the above method but it starts search from given fromIndex.

From string api "Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index."

index = string.indexOf('i', 4);

Output:

index for char d: 8

Searching char 'i' starts from index 4.

Note: indexof(char c) internally calls indexOf​(int ch, int fromIndex) method as indexOf​(ch, 0).

3. lastIndexOf​

3.1 lastIndexOf​(int ch)


lastIndexOf​ method searches the char from index 0 to length-1. But this returns the last match and returns it's index. This method also internally uses charAt method.

this.charAt(k) == ch

In input string "find string", char 'i' is present two times at index 1 and 8. Now, we'll call lastIndexOf​ method and output should the 8 for char 'i'. See the below code.

index = string.lastIndexOf('i');
System.out.println("last index for char i: " + index);

Output:

last index for char i: 8

If no char finds then returns '-1'.

This method works internally search backwards starting index from length-1 to zero.

3.2 lastIndexOf​(int ch, int fromIndex)


This method returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index.

Internal condition:

(this.charAt(k) == ch) && (k <= fromIndex)

Now, we will pass fromIndex as '3' then it will start searching backward from index 3 to 0.

index = string.lastIndexOf('i', 3); 
System.out.println("last index for char i: " + index);

Output:

last index for char i: 1

4 indexOf​(String str)


We'll use the below string in examples from this point.

String newString = "hello java, welcome to java w3schools blog";

4.1 indexOf​(String str)

Returns the index within this string of the first occurrence of the specified substring.

index = newString.indexOf("java");
System.out.println("string \"java\" index found at: " + index);

Output:

string "java" index found at: 6


Internally, this method calls this.startsWith(str, k) method.

4.2 indexOf​(String str, int fromIndex)

Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

index = newString.indexOf("java", 9);
System.out.println("string \"str\" index found at: " + index);

Output:

string "java" index found at: 23

In the above example, seaching will start from index 9. It ignores the match at index 5 and take match from index 23.

Internally, this method calls below code.

k >= Math.min(fromIndex, this.length()) && this.startsWith(str, k)

4.3 lastIndexOf​(String str)


Returns the index within this string of the last occurrence of the specified substring. The last occurrence of the empty string "" is considered to occur at the index value this.length().

index = newString.lastIndexOf("java");
System.out.println("string \"str\" index found at: " + index);

Output: 

lastIndexOf: string "str" index found at: 23

4.4 lastIndexOf​(String str, int fromIndex)


Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index.

index = newString.lastIndexOf("java", 9);
System.out.println("lastIndexOf: string \"str\" index found at: " + index);

Output:

lastIndexOf: string "str" index found at: 6

5. Conclusion

In this article, we've seen how to find the char is present in the string using string api methods such as indexOf, lastIndexOf methods.
Finally, if index is negetive then it will return value '-1'.
indexOf method is used internally in contains(), replace(), split(), isBlank(), indent() methods.


All examples shown in this post are available on GitHub.


0 Comments