str . indexOf(pattern) searches the string str for pattern , which can be either
ID: 3792949 • Letter: S
Question
str.indexOf(pattern) searches the string str for pattern, which can be either a character or a string. The search starts at the beginning of the string. The method returns the first index at which pattern occurs, or -1 if it is not found before the end of the string.
The second form of the method indexOf allows you to start the search at a given index.
str.indexOf(pattern, k) searches the string str for pattern, starting at index k and searching through the rest of the string till the end. If the pattern is found, it returns the index where it is first found, or -1 if it is not found before the end of the string.
1.
String str = "hello, world";
str.length() returns _________________
str.indexOf(‘h’) returns _______________
str.indexOf(‘o’) returns _______________
str.indexOf(‘o’, 5) returns _______________
str.indexOf(‘o’, 9) returns _______________
Explanation / Answer
What is your question, please clarify? Do you just need the answers or you need to write a function that returns that? If you need just answers to above function, they are as follows:
str = "hello, world";
str.length() returns 12
str.indexOf(‘h’) returns 0
str.indexOf(‘o’) returns 4
str.indexOf(‘o’, 5) returns 8
If you need a function that will return above answers, please specify the language in which you need it, I hope we will be able to help you.
Thanks
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.