Bird
0
0

Given a string data = "ID:12345;Name:John;Age:30", which code snippet correctly extracts the value of "Name"?

hard🚀 Application Q8 of 15
C Sharp (C#) - Strings and StringBuilder
Given a string data = "ID:12345;Name:John;Age:30", which code snippet correctly extracts the value of "Name"?
Astring name = data.Substring(data.IndexOf("Name:"), 4);
Bstring name = data.Substring(data.IndexOf("Name:") + 5, data.IndexOf(";Age") - data.IndexOf("Name:") - 5);
Cstring name = data.Substring(data.IndexOf("Name:") + 5, 5);
Dstring name = data.Substring(data.IndexOf("Name:"), data.IndexOf(";Age") - data.IndexOf("Name:"));
Step-by-Step Solution
Solution:
  1. Step 1: Locate start index of "Name:" plus its length

    IndexOf("Name:") + 5 skips "Name:" to start at actual name.
  2. Step 2: Calculate length by subtracting start from index of ";Age"

    Length = IndexOf(";Age") - (IndexOf("Name:") + 5) extracts "John" correctly.
  3. Final Answer:

    string name = data.Substring(data.IndexOf("Name:") + 5, data.IndexOf(";Age") - data.IndexOf("Name:") - 5); -> Option B
  4. Quick Check:

    Use IndexOf and Substring with correct offsets [OK]
Quick Trick: Add length of key to start index for correct substring [OK]
Common Mistakes:
MISTAKES
  • Not adding 5 to skip "Name:"
  • Using wrong length calculation
  • Extracting key name instead of value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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