0
0
C Sharp (C#)programming~5 mins

String searching and extraction in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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#?
AContains()
BIndexOf()
CSubstring()
DLastIndexOf()
What does Substring(3, 4) do on the string "HelloWorld"?
A"oWol"
BloWo"
C"loWo"
D"loWo
If IndexOf() returns -1, what does it mean?
ASubstring was not found
BSubstring is empty
CSubstring was found at the start
DSubstring is at the end
Which method checks if a string contains another string and returns true or false?
ALastIndexOf()
BSubstring()
CIndexOf()
DContains()
What exception is thrown if Substring() is called with an invalid index?
AArgumentOutOfRangeException
BInvalidOperationException
CIndexOutOfRangeException
DNullReferenceException
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.