Bird
0
0

You have a string $data = " red; green ; light blue ; yellow ". How do you split it into an array of clean (trimmed) color names using PowerShell?

hard📝 Application Q15 of 15
PowerShell - String Operations
You have a string $data = " red; green ; light blue ; yellow ". How do you split it into an array of clean (trimmed) color names using PowerShell?
A$data.Split(';') | ForEach-Object { $_.Trim() }
B$data.Replace(' ', '').Split(';')
C$data.Split(';').Trim()
D$data.Trim().Split(';')
Step-by-Step Solution
Solution:
  1. Step 1: Split string by semicolon

    Use $data.Split(';') to get array: [" red", " green ", " light blue ", " yellow "] with spaces.
  2. Step 2: Trim spaces from each element

    Pipe to ForEach-Object with .Trim() to remove spaces from each color name.
  3. Step 3: Check other options

    The option $data.Split(';').Trim() tries to call .Trim() on the array (invalid). The option $data.Replace(' ', '').Split(';') removes all spaces, merging multi-word colors like "light blue" into "lightblue" (wrong). The option $data.Trim().Split(';') trims only ends of the whole string.
  4. Final Answer:

    $data.Split(';') | ForEach-Object { $_.Trim() } -> Option A
  5. Quick Check:

    Split then trim each element = clean array [OK]
Quick Trick: Split first, then trim each part separately [OK]
Common Mistakes:
  • Calling .Trim() on array directly
  • Removing all spaces inside words
  • Trimming only the whole string, not parts

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes