C# How To Reverse a String
Reverse a String
Put the characters in an array, and use the Array.Reverse() method to reverse them:
Example
string text = "Hello";
char[] chars = text.ToCharArray();
Array.Reverse(chars);
string reversed = new string(chars);
Console.WriteLine(reversed);
You can also reverse a string with a for loop that goes backward:
Example
string text = "Hello";
string reversed = "";
for (int i = text.Length - 1; i >= 0; i--)
{
reversed += text[i];
}
Console.WriteLine(reversed);
Explanation:ToCharArray() puts the characters of the string in an array. Array.Reverse() reverses the order of the array, and new string(chars) turns it back into a string.
The loop starts at the last character (text.Length - 1) and adds the characters to reversed one by one, from the end to the start.