Discover how to make your programs find words and phrases instantly, like magic!
Why String searching and extraction in C Sharp (C#)? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a long letter or email, and you want to find a specific word or phrase inside it. Doing this by reading every single word slowly is tiring and takes a lot of time.
Manually scanning through text is slow and easy to make mistakes. You might miss the word or get confused if it appears many times. It's like looking for a needle in a haystack without any tools.
String searching and extraction lets the computer quickly find and pull out the exact words or parts you want. It's like having a smart highlighter that instantly points to what matters.
string text = "Hello world"; if (text.Contains("world")) { Console.WriteLine("Found it!"); }
int index = text.IndexOf("world"); if (index != -1) { string found = text.Substring(index, 5); Console.WriteLine(found); }
This makes it easy to quickly find, check, and use parts of text in programs, saving time and avoiding errors.
Think about searching your email inbox for messages from a friend or extracting phone numbers from a list of contacts automatically.
Manually searching text is slow and error-prone.
String searching and extraction lets computers find text fast and accurately.
This helps automate tasks like searching and pulling out important information.
Practice
IndexOf method return if the searched substring is not found in a string?Solution
Step 1: Understand
TheIndexOfbehaviorIndexOfmethod 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,IndexOfreturns -1 to indicate absence.Final Answer:
-1 -> Option BQuick Check:
IndexOfreturns -1 if substring missing [OK]
- Thinking it returns 0 when not found
- Expecting null instead of -1
- Assuming it throws an error if missing
text?Solution
Step 1: Recall
Substringmethod signatureSubstringtakes 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 AQuick Check:
Correct method and parameters = text.Substring(3, 5); [OK]
- Using wrong method name like Substr or SubString
- Swapping start index and length parameters
- Using only one parameter when two are needed
string s = "hello world";
int pos = s.IndexOf("world");
string part = s.Substring(pos, 5);
Console.WriteLine(part);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
Substring(6, 5) extracts "world" exactly.Final Answer:
world -> Option CQuick Check:
IndexOf + Substring extracts "world" [OK]
- Using wrong start index for Substring
- Confusing length parameter
- Expecting output to include extra characters
string s = "example";
int pos = s.IndexOf("z");
string part = s.Substring(pos, 3);
Console.WriteLine(part);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 AQuick Check:
Negative index in Substring causes error [OK]
- Assuming IndexOf throws exception when not found
- Ignoring negative index causes Substring error
- Blaming Console.WriteLine for error
string sentence. Which code correctly extracts the first word assuming words are separated by spaces?Solution
Step 1: Find position of first space
UseIndexOf(' ')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 DQuick Check:
Check for space, then substring from start [OK]
- Not handling case when no space exists
- Extracting substring after space instead of before
- Using wrong substring parameters
