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

String searching and extraction in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - String searching and extraction
Start with input string
Search for substring or pattern
Is substring found?
NoReturn -1 or empty
Yes
Extract substring or index
Use extracted data
End
The program starts with a string, searches for a substring or pattern, checks if found, then extracts or returns the index, and finally uses the extracted data.
Execution Sample
C Sharp (C#)
string text = "Hello, world!";
int index = text.IndexOf("world");
string extracted = index >= 0 ? text.Substring(index, 5) : "";
Console.WriteLine(extracted);
This code searches for "world" in the text, extracts it if found, and prints it.
Execution Table
StepActionEvaluationResult
1Set text to "Hello, world!"N/Atext = "Hello, world!"
2Search for "world" using IndexOftext.IndexOf("world")index = 7
3Check if index >= 07 >= 0True
4Extract substring from index 7 length 5text.Substring(7, 5)extracted = "world"
5Print extracted substringConsole.WriteLine(extracted)Output: world
💡 Extraction and printing done; program ends.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
text"Hello, world!""Hello, world!""Hello, world!""Hello, world!"
indexundefined777
extractedundefinedundefined"world""world"
Key Moments - 2 Insights
Why do we check if index >= 0 before extracting?
Because IndexOf returns -1 if the substring is not found. Checking index >= 0 (see step 3) ensures we only extract when the substring exists.
What happens if the substring is not found?
If not found, index is -1, so the condition in step 3 is false, and extraction is skipped, resulting in an empty string.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'index' after step 2?
A7
B-1
C0
D5
💡 Hint
Check the 'Result' column in step 2 of the execution_table.
At which step does the program decide if it will extract the substring?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Evaluation' column in step 3 where the condition index >= 0 is checked.
If the substring "world" was not in the text, what would 'extracted' be after step 4?
A"world"
B"Hello"
C"" (empty string)
Dnull
💡 Hint
Refer to the key moment about what happens if substring is not found and the condition in step 3.
Concept Snapshot
String searching uses methods like IndexOf to find substring positions.
If found (index >= 0), use Substring to extract part of the string.
If not found, handle by returning empty or special value.
Always check index before extracting to avoid errors.
Use extracted string as needed (e.g., print or store).
Full Transcript
This example shows how to search for a substring "world" inside a string "Hello, world!" using C# methods. First, the program sets the text variable. Then it uses IndexOf to find the starting position of "world". If the substring is found (index is 7), it extracts the substring starting at that index with length 5. Finally, it prints the extracted substring. If the substring was not found, the program would skip extraction and print an empty string. This step-by-step process helps avoid errors and correctly extracts parts of strings.