Recall & Review
beginner
What method in C# can you use to find the first position of a substring within a string?
You can use the
IndexOf() method. It returns the zero-based index of the first occurrence of the substring, or -1 if not found.Click to reveal answer
beginner
How do you extract a part of a string in C#?
Use the
Substring(startIndex, length) method. It returns a new string starting at startIndex and continuing for length characters.Click to reveal answer
intermediate
What does
LastIndexOf() do in string searching?It finds the last occurrence of a substring in a string and returns its index. If the substring is not found, it returns -1.
Click to reveal answer
beginner
How can you check if a string contains a certain substring in C#?
Use the
Contains() method. It returns true if the substring is found anywhere in the string, otherwise false.Click to reveal answer
intermediate
What happens if you use
Substring() with a startIndex that is out of the string's range?It throws an
ArgumentOutOfRangeException. Always ensure startIndex and length are within the string's bounds.Click to reveal answer
Which method returns the index of the first occurrence of a substring in C#?
✗ Incorrect
IndexOf() returns the position of the first match, while Contains() returns a boolean.What does
Substring(3, 4) do on the string "HelloWorld"?✗ Incorrect
Starting at index 3 (0-based), it extracts 4 characters: 'l', 'o', 'W', 'o'.
If
IndexOf() returns -1, what does it mean?✗ Incorrect
-1 means the substring does not exist in the string.
Which method checks if a string contains another string and returns true or false?
✗ Incorrect
Contains() returns a boolean indicating presence of the substring.What exception is thrown if
Substring() is called with an invalid index?✗ Incorrect
ArgumentOutOfRangeException is thrown when indexes are outside the string's range.Explain how to find and extract a substring from a string in C#.
Think about first locating the substring, then extracting it.
You got /3 concepts.
Describe what happens if you try to extract a substring with invalid indexes in C#.
Consider what the program does when indexes are outside the string length.
You got /3 concepts.