0
0
PowerShellscripting~20 mins

String methods (.Split, .Replace, .Trim) in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Methods Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this PowerShell code using .Split()?
Consider the following PowerShell command:
$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() }
A
apple
banana
cherry
date
Bapple,banana, cherry, date
C
apple
banana, cherry
 date
Dapple banana cherry date
Attempts:
2 left
💡 Hint
Remember that .Split(',') breaks the string at commas, and .Trim() removes spaces around each piece.
💻 Command Output
intermediate
2:00remaining
What does this PowerShell code output after .Replace()?
Look at this code:
$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
AI love cats and dogs love me.
BI love cats and cats love me.
CI love dogs and cats love me.
DI love dogs and dogs love me.
Attempts:
2 left
💡 Hint
The .Replace() method changes all occurrences of the first string to the second string.
📝 Syntax
advanced
2: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?
A$trimmed = $inputString.Trim()
B$trimmed = $inputString.Trim(' ')
C$trimmed = $inputString.TrimStart()
D$trimmed = $inputString.TrimEnd()
Attempts:
2 left
💡 Hint
Think about which method removes spaces from both sides, not just one side.
💻 Command Output
advanced
2:00remaining
What is the output of this PowerShell code using .Split() with multiple characters?
Given this code:
$data = 'one;two,three four'
$parts = $data.Split(';', ',', ' ')

What will be the output?
PowerShell
$data = 'one;two,three four'
$parts = $data.Split(';', ',', ' ')
$parts
A@('one', 'two,three four')
B@('one', 'two', 'three', 'four')
C@('one;two,three four')
D@('one;two', 'three four')
Attempts:
2 left
💡 Hint
The .Split() method can take multiple separator characters to split on any of them.
💻 Command Output
expert
2:00remaining
What is the value of $result after chaining .Replace() and .Trim()?
Analyze this code:
$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
AHello World!
B!llehSrewoP olleH
CHello PowerShell!
D!dlroW olleH
Attempts:
2 left
💡 Hint
The .Replace() changes text inside, and .Trim() removes spaces outside.