C#实现字符串反转的4种方法
见过不少人、经过不少事、也吃过不少苦,感悟世事无常、人心多变,靠着回忆将往事串珠成链,聊聊感情、谈谈发展,我慢慢写、你一点一点看......
1、string.Reverse 方法
string content ="Hello World";
string reverseString = new string(content.Reverse().ToArray());
2、Array.Reverse 方法
string content ="Hello World";
Char[] charArray = content.ToCharArray();
Array.Reverse(charArray);
string reverseString = new string(charArray);
3、for 循环法
string content ="Hello World";
Char[] charArray = content.ToCharArray();
StringBuilder reverseString = new StringBuilder();
for (int i = (charArray.Length - 1); i >= 0; i--)
{
reverseString.Append(charArray[i]);
}
Console.WriteLine(reverseString.ToString());
4、foreach 循环法
string content ="Hello World";
Char[] charArray = content.ToCharArray();
string reverseString = string.Empty;
foreach (char c in charArray)
{
reverseString = c + reverseString;
}
Console.WriteLine(reverseString);
关注我,不失联。有啥问题请留言。
感情恋爱合集https://blog.csdn.net/forever8341/category_12863789.html
职业发展故事https://blog.csdn.net/forever8341/category_12863790.html
常用代码片段https://blog.csdn.net/forever8341/category_12863793.html
程序开发教程https://blog.csdn.net/forever8341/category_12863792.html
自我备考经验 https://blog.csdn.net/forever8341/category_12863791.html
高阶高效代码https://blog.csdn.net/forever8341/category_12873345.html
金融语言解析https://blog.csdn.net/forever8341/category_12877262.html