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
String Searching and Extraction
📖 Scenario: You work in a customer service team. You receive messages from customers that include their order IDs inside the text. You want to find and extract these order IDs to help track their orders quickly.
🎯 Goal: Build a simple C# program that searches a message for an order ID and extracts it.
📋 What You'll Learn
Create a string variable with a customer message containing an order ID.
Create a string variable for the order ID prefix to search for.
Use string searching to find the order ID in the message.
Extract the order ID substring from the message.
Print the extracted order ID.
💡 Why This Matters
🌍 Real World
Customer service teams often need to find order IDs or tracking numbers inside messages to help customers quickly.
💼 Career
Knowing how to search and extract parts of text is useful for data processing, automation, and building user-friendly tools.
Progress0 / 4 steps
1
Create the customer message string
Create a string variable called message and set it to the exact text: "Hello, my order ID is ORD12345. Can you check the status?"
C Sharp (C#)
Hint
Use double quotes to create the string and assign it to message.
2
Create the order ID prefix variable
Create a string variable called orderPrefix and set it to the exact value "ORD".
C Sharp (C#)
Hint
This prefix helps us find where the order ID starts in the message.
3
Find and extract the order ID
Use IndexOf on message with orderPrefix to find the start index. Then use Substring to extract 8 characters starting from that index and save it in a string variable called orderId.
C Sharp (C#)
Hint
Use IndexOf to find where "ORD" starts, then Substring to get the full order ID which has 8 characters.
4
Print the extracted order ID
Write a Console.WriteLine statement to print the orderId variable.
C Sharp (C#)
Hint
This will show the extracted order ID on the screen.
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
Step 1: Understand IndexOf behavior
The IndexOf method returns the zero-based index of the first occurrence of the substring if found.
Step 2: Check return value when substring is missing
If the substring is not found, IndexOf returns -1 to indicate absence.
Final Answer:
-1 -> Option B
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
Step 1: Recall Substring method signature
Substring takes start index first, then length: Substring(int startIndex, int length).
Step 2: Match correct syntax
text.Substring(3, 5); uses correct method name and parameter order: start at 3, length 5.
Final Answer:
text.Substring(3, 5); -> Option A
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
Step 1: Find index of "world" in string
"world" starts at index 6 in "hello world".
Step 2: Extract substring from index 6 with length 5
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
Step 1: Check IndexOf result for "z"
"z" is not in "example", so IndexOf returns -1.
Step 2: Substring called with start index -1 causes exception
Substring cannot start at negative index, so it throws ArgumentOutOfRangeException.
Final Answer:
Substring called with negative start index -> Option A
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
Step 1: Find position of first space
Use IndexOf(' ') to find where the first space is.
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.
Final Answer:
int spacePos = sentence.IndexOf(' ');
string firstWord = spacePos == -1 ? sentence : sentence.Substring(0, spacePos); -> Option D
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