Bird
Raised Fist0
C Sharp (C#)programming~5 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

      Practice

      (1/5)
      1. What does the IndexOf method return if the searched substring is not found in a string?
      easy
      A. 0
      B. -1
      C. null
      D. An exception is thrown

      Solution

      1. Step 1: Understand IndexOf behavior

        The IndexOf method returns the zero-based index of the first occurrence of the substring if found.
      2. Step 2: Check return value when substring is missing

        If the substring is not found, IndexOf returns -1 to indicate absence.
      3. Final Answer:

        -1 -> Option B
      4. Quick Check:

        IndexOf returns -1 if substring missing [OK]
      Hint: Remember: Not found means -1 from IndexOf [OK]
      Common Mistakes:
      • Thinking it returns 0 when not found
      • Expecting null instead of -1
      • Assuming it throws an error if missing
      2. Which of the following is the correct syntax to extract a substring starting at index 3 with length 5 from a string text?
      easy
      A. text.Substring(3, 5);
      B. text.SubString(5, 3);
      C. text.Substring(5);
      D. text.Substr(3, 5);

      Solution

      1. Step 1: Recall Substring method signature

        Substring takes start index first, then length: Substring(int startIndex, int length).
      2. Step 2: Match correct syntax

        text.Substring(3, 5); uses correct method name and parameter order: start at 3, length 5.
      3. Final Answer:

        text.Substring(3, 5); -> Option A
      4. Quick Check:

        Correct method and parameters = text.Substring(3, 5); [OK]
      Hint: Remember: Substring(startIndex, length) with capital S [OK]
      Common Mistakes:
      • Using wrong method name like Substr or SubString
      • Swapping start index and length parameters
      • Using only one parameter when two are needed
      3. What is the output of this code?
      string s = "hello world";
      int pos = s.IndexOf("world");
      string part = s.Substring(pos, 5);
      Console.WriteLine(part);
      medium
      A. hello
      B. worldd
      C. world
      D. error

      Solution

      1. Step 1: Find index of "world" in string

        "world" starts at index 6 in "hello world".
      2. Step 2: Extract substring from index 6 with length 5

        Substring(6, 5) extracts "world" exactly.
      3. Final Answer:

        world -> Option C
      4. Quick Check:

        IndexOf + Substring extracts "world" [OK]
      Hint: IndexOf finds start, Substring extracts exact length [OK]
      Common Mistakes:
      • Using wrong start index for Substring
      • Confusing length parameter
      • Expecting output to include extra characters
      4. The following code throws an exception. What is the main cause?
      string s = "example";
      int pos = s.IndexOf("z");
      string part = s.Substring(pos, 3);
      Console.WriteLine(part);
      medium
      A. Substring called with negative start index
      B. IndexOf throws exception if not found
      C. Length parameter is too large
      D. Console.WriteLine cannot print substrings

      Solution

      1. Step 1: Check IndexOf result for "z"

        "z" is not in "example", so IndexOf returns -1.
      2. Step 2: Substring called with start index -1 causes exception

        Substring cannot start at negative index, so it throws ArgumentOutOfRangeException.
      3. Final Answer:

        Substring called with negative start index -> Option A
      4. Quick Check:

        Negative index in Substring causes error [OK]
      Hint: Check if IndexOf is -1 before using Substring [OK]
      Common Mistakes:
      • Assuming IndexOf throws exception when not found
      • Ignoring negative index causes Substring error
      • Blaming Console.WriteLine for error
      5. You want to extract the first word from a sentence stored in string sentence. Which code correctly extracts the first word assuming words are separated by spaces?
      hard
      A. string firstWord = sentence.Substring(sentence.IndexOf(' ') + 1);
      B. string firstWord = sentence.Substring(0, sentence.IndexOf(' '));
      C. int spacePos = sentence.IndexOf(' '); string firstWord = sentence.Substring(spacePos);
      D. int spacePos = sentence.IndexOf(' '); string firstWord = spacePos == -1 ? sentence : sentence.Substring(0, spacePos);

      Solution

      1. Step 1: Find position of first space

        Use IndexOf(' ') to find where the first space is.
      2. Step 2: Extract substring from start to space or whole string if no space

        If no space found (-1), the whole sentence is one word; else extract from 0 to space position.
      3. Final Answer:

        int spacePos = sentence.IndexOf(' '); string firstWord = spacePos == -1 ? sentence : sentence.Substring(0, spacePos); -> Option D
      4. Quick Check:

        Check for space, then substring from start [OK]
      Hint: Check if space exists before substring to avoid errors [OK]
      Common Mistakes:
      • Not handling case when no space exists
      • Extracting substring after space instead of before
      • Using wrong substring parameters