C#完成字符串倒序的寫法。本站提示廣大學習愛好者:(C#完成字符串倒序的寫法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成字符串倒序的寫法正文
本文實例為年夜家分享了C#字符串倒序寫法的完成代碼,供年夜家參考,詳細內容以下
//string concatenation with for loop public string ReverseA(string text) { char[] cArray = text.ToCharArray(); string reverse = String.Empty; for (int i = cArray.Length - 1; i > -1; i--) { reverse += cArray[i]; } return reverse; } // Array.Reverse function public string ReverseB(string text) { char[] charArray = text.ToCharArray(); Array.Reverse(charArray); return new string(charArray); } // push/pop Stack<> public string ReverseC(string text) { Stack resultStack = new Stack(); foreach (char c in text) { resultStack.Push(c); } StringBuilder sb = new StringBuilder(); while (resultStack.Count > 0) { sb.Append(resultStack.Pop()); } return sb.ToString(); } // LINQ public string ReverseD(string text) { return new string(text.ToCharArray().Reverse().ToArray()); } // StringBuilder public string ReverseE(string text) { char[] cArray = text.ToCharArray(); StringBuilder reverse = new StringBuilder(); for (int i = cArray.Length - 1; i > -1; i--) { reverse.Append(cArray[i]); } return reverse.ToString(); }
以上就是本文的全體內容,願望對年夜家進修C#法式設計有所贊助。