方法一:
public static string Reverse(string name)
{
if (String.IsNullOrEmpty(name))
{
throw new Exception("字符串不能为空!");
}
StringBuilder sb = new StringBuilder(name.Length);
for (int i = name.Length-1; i >= 0; i--)
{
sb.Append(name[i]);
}
return sb.ToString();
}
方法二:.NET3.5以上
public static string Reverse(string name)
{
char[] reverse = name.Reverse().ToArray();
return new string(reverse);
}
方法三:二分法
public static string Reverse(string name)
{
if (String.IsNullOrEmpty(name))
{
throw new Exception("字符串不能为空!");
}
char[] nm = name.ToCharArray();
for (int i = 0; i < (nm.Length-1 )/ 2; i++)
{
char q = nm[i];
nm[i]= nm[nm.Length - 1 - i];
nm[nm.Length - 1 - i] = q;
}
return new string(nm);
}