public sealed class Program { static void Main(string[] args) { string text = "1234567"; Console.WriteLine("需要转换的数字:{0}",text); string result = string.Empty; //第一种写法,循环输出 char[] cArray = text.ToCharArray(); for (int i = cArray.Length - 1; i >= 0; i--) { result += cArray[i]; } Console.WriteLine("循环输出写法:{0}",result); //第二种写法,Array的反转 char[] charArray = text.ToCharArray(); Array.Reverse(charArray); result = new string(charArray); Console.WriteLine("Array反转写法:{0}",result); //第三种写法,先进后出集合 Stack resultStack = new Stack(); foreach (char c in text) { resultStack.Push(c); } StringBuilder sb = new StringBuilder(); foreach (var item in resultStack) { sb.Append(item); } Console.WriteLine("先进后出写法:{0}",sb.ToString()); //第四种写法,Linq反转 result = new string(text.ToArray().Reverse().ToArray()); Console.WriteLine("Linq的Array反转:{0}",result); Console.ReadLine(); } }
时间: 2024-10-11 03:26:14