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);
}
===================
Tags: string reverse in C#
.Net asp .net tutorial asp .net tutorial c# sample code CSS sample script database guide java sample code javascript sample code mssql sample script string reverse in C# t-sql telerik sample code user-defined function vb .net sample code visual studio tutorial
WP Cumulus Flash tag cloud by Roy Tanck requires Flash Player 9 or better.
RSS feed for comments on this post · TrackBack URI
Leave a reply