Reversing a string in C# is quite easy. The main concept is to treat the string as an array of characters, which is already built-in in C#, and transfer each character to new container in reverse order. Here are the samples of simple and straightforward functions on how to reverse a string in C#.

Manual reverse transfer using for-loop

public static string ReverseByTransfer(string str)
{
int strlen = str.Length;
char[] arr = new char[strlen];

for (int i = 1; i <= strlen ; i++)
{
arr[i-1] = str[strlen - i];
}

return new string(arr);
}

Convert string to character-array and use Array’s Reverse function

public static string ReverseByConvert(string str)
{
char[] arr = str.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}

DOWNLOAD REVERSE STRING CODE

===================

Tags: