Skip to content

Instantly share code, notes, and snippets.

@arebee
Created September 30, 2014 18:26
Show Gist options
  • Save arebee/ee72fed0a7b5eb90f380 to your computer and use it in GitHub Desktop.
Save arebee/ee72fed0a7b5eb90f380 to your computer and use it in GitHub Desktop.
Reverse a string in PowerShell, using an efficient StringBuilder allocation.
function reverseString
{
Param(
[string]$str
)
$sb = New-Object System.Text.StringBuilder($str.Length)
write-verbose $sb.Capacity
for ($i = ($str.Length - 1); $i -ge 0; $i--)
{
[void]$sb.Append($str.Chars($i))
}
return $sb.ToString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment