Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • 2
    Nice. As a performance tweak you could keep track of the first non-whitespace character's position in a run and then use substring to copy the whole run rather than character by character in every iteration - it's swings and roundabouts though as it makes the code a bit more complicated, but if $Length is large it might be worth it... Commented Jul 24, 2024 at 13:35
  • 1
    Also, the whitespace class - \s - has a few extra characters in it - learn.microsoft.com/en-us/dotnet/standard/base-types/…, if that matters to the OP... Commented Jul 24, 2024 at 13:36
  • 1
    @mclayton not sure using substring would be a performance boost since it's already processing char by char and appending to a stringbuilder is already very efficient. substring would have to iterate over the original string once again when this is already doing that. as for the rest of whitespace character, i.e. a tab, I'm not sure how iRon wants to deal with those. will wait for his feedback. Commented Jul 24, 2024 at 13:41
  • 3
    Nice, came here to suggest the same ^_^ FWIW you can simplify the branching logic with a single elseif([char]::IsWhiteSpace()){ if(!$prevSpace){$sb.Append(' ')} $prevSpace = $true } block in the middle Commented Jul 24, 2024 at 15:14
  • 1
    that's much better @MathiasR.Jessen ! thanks Commented Jul 24, 2024 at 15:24