Bird
Raised Fist0

Which method would you use to find the first occurrence index of a substring in a string?

easy🧠 Conceptual Q2 of Q15
C Sharp (C#) - Strings and StringBuilder
Which method would you use to find the first occurrence index of a substring in a string?
ASubstring()
BIndexOf()
CContains()
DReplace()
Step-by-Step Solution
Solution:
  1. Step 1: Identify method for position search

    IndexOf returns the zero-based index of the first occurrence of a substring in a string.
  2. Step 2: Differentiate from other methods

    Substring extracts part of string, Contains returns boolean, Replace changes text.
  3. Final Answer:

    IndexOf() -> Option B
  4. Quick Check:

    IndexOf method = position finder [OK]
Quick Trick: IndexOf returns position of substring or -1 [OK]
Common Mistakes:
MISTAKES
  • Using Substring to find position
  • Confusing Contains with IndexOf
  • Expecting Replace to find substring

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes