+20翻转字符.
static void Main(string[] args)
{
string m=null;
string str = "hello world ";
char[] chars = str.ToCharArray();
foreach(char j in chars)
{
m+=j;
}
System.Console.WriteLine( "{0} ",m);
}
我想让程序输出:dlrow olleh
不想用Array.Reverse
------解决方案--------------------try
string m = string.Empty;
string str = "hello world ";
StringBuilder sb = new StringBuilder();
for (int i = str.Length - 1; i > = 0; i--)
{
sb.Append(str[i]);
}
m = sb.ToString();
System.Console.WriteLine( "{0} ", m);
------解决方案--------------------static void Main(string[] args)
{
string m = null;
string str = "hello world ";
char[] chars = str.ToCharArray();
for (int i=chars.Length-1;i> =0;i--)
{
m += chars[i];
}
System.Console.WriteLine( "{0} ", m);
}
------解决方案--------------------string str = "hello world ";
string temp = " ";
for(int i = 0; i <str.Length;i++)
temp += str[str.Length - 1 - i];
Console.WriteLine(temp);
------解决方案--------------------string str = "hello world ";
string revStr = string.Empty;
for (int i= str.Length-1; i> =0; i--)
{
revStr += str[i];
}
Console.WriteLine(revStr);
------解决方案--------------------private string StringReverse(string inStr)
{
string temp = string.Empty;
int length = inStr.Length;
for (int i = 0; i < (length / 2); ++i)
{
temp = inStr[i];
inStr[i] = inStr[length - i - 1];
inStr[length - i - 1] = temp;
}
return inStr;
}
------解决方案--------------------用VB 库的 StrReverse 颠倒字符串位置
.NET 可以
Function StringReverse(ByVal s As String, Optional ByVal stringIndex As Integer = 0, Optional ByVal count As Integer = -1)
Dim chars() As Char = s.ToCharArray()
If count < 0 Then count = s.Length - stringIndex
Array.Reverse(chars, stringIndex, count)
Return New String(chars)
End Function