反转字条串的算法

string test= "abcdefgh";

1. 字符串倒序读取

            string result = string.Empty;
            for (int i = test.Length - 1; i >= 0; i--)
            {
                result += test[i];
            }

2. 二分法转换

            string result = string.Empty;

            char[] arrTest = test.ToCharArray();
            for (int i = 0; i < arrTest .Length / 2; i++)
            {
                char temp = arrTest [i];
                arrTest [i] = arrTest [arrTest.Length - i - 1];
                arrTest [arrTest .Length - i - 1] = temp;
            }
            result = new string(arrTest);

posted on 2012-03-20 09:07  thankyou  阅读(177)  评论(1编辑  收藏  举报