在网上看到人家写的一个字符反序的算法,
于是自己也花上那么一两分钟写一个,欢迎提出更优化的算法供大家一起学习学习.
代码
只给个方法名,具体的命名空间就不用给了.
private string Rev(string str)
{
int len = 0;
if (str == null || (len = str.Length) < 2)
return str;
char[] c = str.ToCharArray();
char tmp = 'c';
for (int i = 0; i < len / 2; i++)
{
tmp = c[i];
c[i] = str[len - i - 1];
c[len - i - 1] = tmp;
}
return new string(c);
}
{
int len = 0;
if (str == null || (len = str.Length) < 2)
return str;
char[] c = str.ToCharArray();
char tmp = 'c';
for (int i = 0; i < len / 2; i++)
{
tmp = c[i];
c[i] = str[len - i - 1];
c[len - i - 1] = tmp;
}
return new string(c);
}