Monthly Archive

Categories

Reverse a string

I sort of brushed over it on my last post but this is how you reverse a string.

 

function get-reversestring {
[CmdletBinding()]
param (
[string]$teststring
)

$ca = $teststring.ToCharArray()

[array]::Reverse($ca)

-join $ca
}

 

Take the input string and turn it into an array of chars. Use the reverse static method of the array class to reverse the array. Other useful array methods can be found in the documentation - https://docs.microsoft.com/en-us/dotnet/api/system.array?redirectedfrom=MSDN&view=netframework-4.7.2

-Join is used to stitch the reversed char array back into a string.

Comments are closed.