String is an object with sequence of characters. In PowerShell, strings are represented inside double quotes (“”) or single quotes (”).
The characters in a string objects are accessible by their respective index.
$var1 = "Good Morning Everyone" # To print the first character $var1 # To print the last character $var1[-1] #or $var1[$var1.Length-1]
To print the string as sequence of characters
As the string is a character array, we can reverse the string by –
1. Accessing the string index from last to first and joining the characters
$var1 = "Good Morning Everyone" $var1[$var1.Length..0] -join "" # OR $var1[-1..-$var1.Length ] -join ""
2. Using reverse method in arrays
$var1 = "Good Morning Everyone" # Converting the string to character array $temp = $var1.ToCharArray() [array]::Reverse($temp) # Joining the reversed array to string $temp -join ""
If you know of other ways to reverse the string in PowerShell, comment below with the code !!!
Watch out for the next post about reversing string using Python.
Thank you for reading the article.