This simple post will help you to reverse the C# string. This method loop through all the characters of string and construct the new string in reverse order.
To use this function pass the given string in this method and it will return the reversed string as output.
private string ReverseStr(string szLine)
{
int index;
string strTemp;
string strA;
strTemp = "";
try
{
for (index = szLine.Length; index >= 1; index--)
{
strA = szLine.Substring(index - 1, 1);
strTemp = strTemp + strA;
}
}
catch (Exception ex)
{
throw;
}
finally
{
}
return strTemp;
}
Another way to do string reversal is:
private string ReverseString(string str)
{
char[] StringArrya = str.ToCharArray();
Array.Reverse(StringArrya);
return new string(StringArrya);
}
1 comments: