What is indexOf() Function?
To check whether a substring is present inside a string or to find out the index number of a specific character inside a string indexOf () function is used. By calling this function, the user can find out where the first occurrence occurs of a character or string. In case if the character you are looking for doesn’t exist inside the string a negative -1 number will be printed on screen.
By default, this function searches index from the beginning of the string however we can start it from a specific number by specifying it in the second parameter of the function.
Syntax
Following is the syntax of indexOf() function:
Command below will find the index of the first occurring specific character inside a string:
Below command will search the index value of the character after starting from a specific number:
Parameters Values
This function has three parameters:
new_string: A variable of type String.
index_value: Value whose index needs to be searched. The allowed data types for value are char and String.
from: The index to begin searching from.
Return Value
The function indexOf() returns the value of index within the String. If char is not found inside string -1 value will be returned.
Example Code
Serial.begin(9600); /*Serial Communication begins*/
String new_string = "Linuxhint Arduino"; /*New string is defined*/
Serial.println(new_string.indexOf('i')); /*Index of first letter i is printed*/
Serial.println(new_string.indexOf("hint")); /*Starting Index of specific string is printed */
Serial.println(new_string.indexOf('i',8)); /*letter i index printed after checking string from 8th character*/
Serial.println(new_string.indexOf('z')); /*Search for letter z will return -1*/
}
void loop() {
}
Here in above code, we initialized a serial communication after that a new string “Linuxhint Arduino” is defined. Next using indexOf() function letter “i” index is printed on serial monitor.
After that we will search for the index value of the substring “hint”. After that we will check the index of the letter “i” but this time the program will search for “i” after the character number 8.
Last line of code will print -1 because no character with letter “z” is found in new_String.
Following illustration of new_String will help to understand code in a better way.
Output
Output will print four numbers.
1: Index of the first occurring “i” is 1 starting from the left side.
5: Index of substring “hint” is 5 starting from left.
14: Index value of letter “i” after starting from the 8th character.
-1: Index value returned by program is -1 because no character with “z” letter is found inside the string.
Note: Another function similar to indexOf() used in Arduino programming is lastIndexOf(). The only difference between the two is that lastindexOf will search for a character or string starting from the end of the string. Like in above function this also assists in finding from a specific index by specifying it in the second parameter of the function.
Conclusion
This article highlights the working of the indexOf() function. Using this function, we can find an index of any specific character or substring inside a string. It takes three parameters and returns the result in integer data type. Result will be negative if the search value is not available inside the main string.