C# Programming - Strings - Discussion

Discussion Forum : Strings - General Questions (Q.No. 17)
17.
Which of the following is the correct way to find out the index of the second 's' in the string "She sells sea shells on the sea-shore"?
String str = "She sells sea shells on the sea-shore"; 
int i;
i = str.SecondIndexOf("s");
String str = "She sells sea shells on the sea-shore"; 
int i, j;
i = str.FirstIndexOf("s"); 
j = str.IndexOf("s", i + 1);
String str = "She sells sea shells on the sea-shore"; 
int i, j;
i = str.IndexOf("s"); 
j = str.IndexOf("s", i + 1);
String str = "She sells sea shells on the sea-shore"; 
int i, j;
i = str.LastIndexOf("s"); 
j = str.IndexOf("s", i - 1);
String str = "She sells sea shells on the sea-shore"; 
int i, j;
i = str.IndexOf("S"); 
j = str.IndexOf("s", i);
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
5 comments Page 1 of 1.

Peter said:   7 years ago
Option E will work too. When string value is passed to IndexOf(), it does a case-sensitive search. So value of i will be 0 and the value of j will be 4, what is a correct answer. Just to be sure I tested it in VS and I get the expected result - values of 0 and 4.

Alex said:   7 years ago
So answer c works, because we are using the overloaded method indexof() that takes in a string and then a starting index in this case.

So j= str.IndexOf("s",i+1)

is taking the index position of the first "s" 0 and saying to look for the index position of the second 4 starting from the character +1 (so "h").
in this way, it gives us the seconds at index 4.

Andy said:   10 years ago
i = str.FirstIndexOf("s"); it doesn't exist.

Kamal said:   1 decade ago
Please explain why following answer is wrong:

String str = "She sells sea shells on the sea-shore";

int i, j;

i = str.FirstIndexOf("s");

j = str.IndexOf("s", i + 1);

Nishant said:   1 decade ago
Indexof returns the position of first occurrence of the the substring. So in this string ..'s' comes first in the position and the position of next 's' can be find out using the str.Indexof("s",i+1).

Post your comments here:

Your comments will be displayed after verification.