This post will discuss how to reverse a string in C#.

1. Using Array.Reverse() method

The idea is to convert the given string into a character array using the String.ToCharArray() method and then reverse the character array in-place using the Array.Reverse() method. Finally, convert the reversed array back to the string using the String constructor. This is demonstrated below:

Download  Run Code

2. Using Enumerable.Reverse() method

The following code example demonstrates how to use LINQ’s Reverse() method for reversing a string in C#.

Download  Run Code

3. Naive solution

Following is another simple way to reverse a string in C#:

  1. Create a character array from given string using ToCharArray method.
  2. Start from the two endpoints of the given array and run the for-loop till two endpoints intersect. In each iteration of the loop, swap values present at two indexes.
  3. Finally, convert the character array back into a string using string constructor.

Download  Run Code

4. Using StringBuilder.Append() method

We can use the StringBuilder.Append() method to reverse a string in C#. The idea is to read characters from the end of the string and append each character to the StringBuilder instance. Finally, call the ToString() method to get the reversed string.

Download  Run Code

That’s all about reversing a string in C#.