The index()
method is used to find the index position of a character or sub-string in a string.
# for a character
index(string, char);
# for a sub-string
index(string, sub-string);
This method returns the index position of the character char
or the sub-string sub-string
in the string string
.
# get index positions of characters and sub-stringsprint index("Educative is great", "E"); # 0print("\n");print index("Shots and authors", "s"); # 4print("\n");print index("Courses and students", "and"); # 8print("\n");print index("Perl is great!", "z"); # -1 not found
E
in the string Educative is great
. Then, we print the index that is returned.s
in the string Shots and authors
and print the returned value.and
in the string Courses and students
and print the returned value.z
in the string Perl is great
and print the returned value.