字符串反转的几种方法

class Program
    {
        static void Main(string[] args)
        {
            string test = "this is test case";
            //except result="esac tset si siht";
            string result1 = Reserve1(test);
            string result2 = Reserve2(test);
            string result3 = Reserve3(test);
        }

        public static string Reserve1(string str)
        {
            string result = null;
            for (int i = str.Length - 1; i >= 0; i--)
            {
                result += str[i];
            }

            return result;
        }

        public static string Reserve2(string str)
        {
            Hashtable hs = new Hashtable();
            int i = 0;
            foreach (char c in str)
            {
                hs.Add(i, c);
                ++i;
            }

            string result = null;
            foreach (DictionaryEntry c in hs)
            {
                result += c.Value;
            }

            return result;
        }

        public static string Reserve3(string str)
        {
            Stack stack = new Stack();
            foreach (char c in str)
            {
                stack.Push(c);
            }
            
            string result = null;
            foreach (var c in stack)
            {
                result += c.ToString();
            }

            return result;
        }
View Code
原文地址:https://www.cnblogs.com/binyao/p/4921718.html