Bird
Raised Fist0
C Sharp (C#)programming~20 mins

Common string methods in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
String Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of ToUpper and Trim methods
What is the output of this C# code snippet?
C Sharp (C#)
string text = "  Hello World  ";
string result = text.Trim().ToUpper();
Console.WriteLine(result);
AHELLO WORLD
BDLROW OLLEH
Chello world
DHello World
Attempts:
2 left
💡 Hint
Remember that Trim removes spaces at the start and end, and ToUpper converts all letters to uppercase.
Predict Output
intermediate
2:00remaining
Result of Replace and Substring methods
What will be printed by this C# code?
C Sharp (C#)
string s = "apple pie";
string newS = s.Replace("pie", "tart").Substring(0, 5);
Console.WriteLine(newS);
Atart
Bapplet
Capple pie
Dapple
Attempts:
2 left
💡 Hint
Replace changes "pie" to "tart" and Substring(0,5) takes the first 5 characters.
Predict Output
advanced
2:00remaining
Output of IndexOf and Contains methods
What is the output of this code?
C Sharp (C#)
string phrase = "Learning C# is fun!";
int index = phrase.IndexOf("C#");
bool hasFun = phrase.Contains("fun");
Console.WriteLine($"{index} {hasFun}");
A9 True
B9 False
C-1 True
D10 True
Attempts:
2 left
💡 Hint
IndexOf returns the first position of the substring or -1 if not found. Contains returns true if substring exists.
Predict Output
advanced
2:00remaining
Effect of ToLower and StartsWith methods
What does this code print?
C Sharp (C#)
string input = "OpenAI ChatGPT";
string lower = input.ToLower();
bool starts = lower.StartsWith("open");
Console.WriteLine(starts);
AFalse
Btrue
CTrue
Dfalse
Attempts:
2 left
💡 Hint
ToLower converts all letters to lowercase. StartsWith checks if string begins with given substring.
Predict Output
expert
3:00remaining
Output of complex string manipulation
What is the output of this C# code?
C Sharp (C#)
string s = "  CSharp Programming  ";
string result = s.Trim().Replace(" ", "_").ToLower();
Console.WriteLine(result);
ACSharp_Programming
Bcsharp programming
Ccsharp_programming
DCSharp Programming
Attempts:
2 left
💡 Hint
Trim removes spaces at ends, Replace changes spaces to underscores, ToLower makes all letters lowercase.

Practice

(1/5)
1. Which C# string method returns a new string with all characters converted to uppercase?
easy
A. Substring()
B. Trim()
C. Contains()
D. ToUpper()

Solution

  1. Step 1: Understand the purpose of ToUpper()

    The ToUpper() method converts all letters in a string to uppercase and returns a new string.
  2. Step 2: Compare with other methods

    Trim() removes spaces, Contains() checks for substring presence, Substring() extracts part of the string.
  3. Final Answer:

    ToUpper() -> Option D
  4. Quick Check:

    Uppercase conversion = ToUpper() [OK]
Hint: Uppercase all letters? Use ToUpper() method [OK]
Common Mistakes:
  • Confusing ToUpper() with ToLower()
  • Using Trim() to change case
  • Thinking Contains() changes text
2. Which of the following is the correct syntax to check if the string text contains the word "hello"?
easy
A. Contains(text, "hello")
B. text.Contains = "hello"
C. text.Contains("hello")
D. text.Has("hello")

Solution

  1. Step 1: Recall the correct method call syntax

    In C#, to check if a string contains another, use the instance method with parentheses: text.Contains("hello").
  2. Step 2: Identify incorrect syntax

    Assignments (=) or wrong method names like Has() are invalid for this check.
  3. Final Answer:

    text.Contains("hello") -> Option C
  4. Quick Check:

    Method call with parentheses = Contains() [OK]
Hint: Use text.Contains("word") with parentheses [OK]
Common Mistakes:
  • Using assignment instead of method call
  • Wrong method name like Has()
  • Passing parameters incorrectly
3. What is the output of the following code?
string s = "  Hello World  ";
string result = s.Trim().Substring(0, 5);
Console.WriteLine(result);
medium
A. " Hel"
B. "Hello"
C. "Hello "
D. "Hello World"

Solution

  1. Step 1: Apply Trim() method

    Trim() removes spaces at the start and end, so " Hello World " becomes "Hello World".
  2. Step 2: Apply Substring(0, 5)

    Substring(0, 5) extracts characters from index 0 to 4, which is "Hello".
  3. Final Answer:

    "Hello" -> Option B
  4. Quick Check:

    Trim + Substring(0,5) = "Hello" [OK]
Hint: Trim removes spaces; Substring extracts exact part [OK]
Common Mistakes:
  • Not trimming before substring
  • Counting spaces in substring
  • Confusing substring length
4. Identify the error in this code snippet:
string s = "Example";
if(s.Contains("ex"))
{
    Console.WriteLine("Found");
}
medium
A. Contains() is case-sensitive, so "ex" won't match "Ex"
B. Missing semicolon after if statement
C. Contains() method does not exist for strings
D. Console.WriteLine syntax is incorrect

Solution

  1. Step 1: Check Contains() behavior

    Contains() is case-sensitive, so "ex" does not match "Ex" in "Example".
  2. Step 2: Verify syntax correctness

    Semicolons and method calls are correct; no syntax errors present.
  3. Final Answer:

    Contains() is case-sensitive, so "ex" won't match "Ex" -> Option A
  4. Quick Check:

    Contains() case matters = true [OK]
Hint: Remember Contains() is case-sensitive by default [OK]
Common Mistakes:
  • Assuming Contains() ignores case
  • Looking for syntax errors that don't exist
  • Confusing method availability
5. You want to extract the domain name from an email string email = "user@example.com". Which code correctly extracts "example" using common string methods?
hard
A. string domain = email.Substring(email.IndexOf('@') + 1).Split('.')[0];
B. string domain = email.Trim().ToUpper();
C. string domain = email.Contains("@example");
D. string domain = email.Substring(0, email.IndexOf('.'));

Solution

  1. Step 1: Find position after '@'

    email.IndexOf('@') finds the '@' position; adding 1 moves to start of domain.
  2. Step 2: Extract substring from domain start and split by '.'

    Substring gets "example.com", then Split('.')[0] gets "example".
  3. Step 3: Check other options

    B changes case, C returns bool, D extracts wrong part before '@'.
  4. Final Answer:

    string domain = email.Substring(email.IndexOf('@') + 1).Split('.')[0]; -> Option A
  5. Quick Check:

    Extract domain by substring + split = string domain = email.Substring(email.IndexOf('@') + 1).Split('.')[0]; [OK]
Hint: Use IndexOf('@') + Substring + Split('.') to get domain [OK]
Common Mistakes:
  • Using Contains() instead of extracting
  • Trimming or changing case wrongly
  • Extracting before '@' instead of after