【转】c#实现字符串倒序的n种写法

其中LINQ写法最为简洁

//string concatenation with for loop
public string ReverseA(string text)
{
    char[] cArray = text.ToCharArray();
    string reverse = String.Empty;
    for (int i = cArray.Length - ; i > -; 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 > )
    {
        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 - ; i > -; i--)
    {
        reverse.Append(cArray[i]);
    }
    return reverse.ToString();
}
上一篇:深入理解 Java 虚拟机之学习笔记(2)


下一篇:Android -- 提交数据到服务器,Get Post方式, 异步Http框架提交