Challenge - 5 Problems
String Methods Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this PowerShell code using .Split()?
Consider the following PowerShell command:
What will be the output?
$text = 'apple,banana, cherry, date'
$result = $text.Split(',')
$result | ForEach-Object { $_.Trim() }
What will be the output?
PowerShell
$text = 'apple,banana, cherry, date' $result = $text.Split(',') $result | ForEach-Object { $_.Trim() }
Attempts:
2 left
💡 Hint
Remember that .Split(',') breaks the string at commas, and .Trim() removes spaces around each piece.
✗ Incorrect
The .Split(',') method splits the string into parts at each comma. Each part may have spaces, so .Trim() removes those spaces. The output is each fruit name on its own line without extra spaces.
💻 Command Output
intermediate2:00remaining
What does this PowerShell code output after .Replace()?
Look at this code:
What is the output?
$sentence = 'I love cats and cats love me.'
$newSentence = $sentence.Replace('cats', 'dogs')
$newSentence
What is the output?
PowerShell
$sentence = 'I love cats and cats love me.' $newSentence = $sentence.Replace('cats', 'dogs') $newSentence
Attempts:
2 left
💡 Hint
The .Replace() method changes all occurrences of the first string to the second string.
✗ Incorrect
The .Replace('cats', 'dogs') replaces every 'cats' with 'dogs' in the string, so both 'cats' words become 'dogs'.
📝 Syntax
advanced2:00remaining
Which option correctly trims spaces from both ends of a string in PowerShell?
You want to remove spaces at the start and end of a string stored in $inputString. Which code does this correctly?
Attempts:
2 left
💡 Hint
Think about which method removes spaces from both sides, not just one side.
✗ Incorrect
The .Trim() method removes whitespace from both the start and end of the string. .TrimStart() and .TrimEnd() remove spaces only from one side.
💻 Command Output
advanced2:00remaining
What is the output of this PowerShell code using .Split() with multiple characters?
Given this code:
What will be the output?
$data = 'one;two,three four'
$parts = $data.Split(';', ',', ' ')
What will be the output?
PowerShell
$data = 'one;two,three four' $parts = $data.Split(';', ',', ' ') $parts
Attempts:
2 left
💡 Hint
The .Split() method can take multiple separator characters to split on any of them.
✗ Incorrect
The .Split(';', ',', ' ') splits the string at any semicolon, comma, or space, resulting in four separate strings.
💻 Command Output
expert2:00remaining
What is the value of $result after chaining .Replace() and .Trim()?
Analyze this code:
What is the value of $result?
$text = ' Hello World! '
$result = $text.Replace('World', 'PowerShell').Trim()
$result
What is the value of $result?
PowerShell
$text = ' Hello World! ' $result = $text.Replace('World', 'PowerShell').Trim() $result
Attempts:
2 left
💡 Hint
The .Replace() changes text inside, and .Trim() removes spaces outside.
✗ Incorrect
First, 'World' is replaced with 'PowerShell', so the string becomes ' Hello PowerShell! '. Then .Trim() removes spaces at start and end, leaving 'Hello PowerShell!'.