• 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 - 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();
    }

  • 相关阅读:
    Linux下定时删除指定目下n天前的文件
    日期时间格式化
    sed与awk
    Linux守护进程(init.d和xinetd)
    python-Json模块
    python3 urllib模块
    linux 命令 rsync
    Linux下scp的用法
    代码块重定向
    使用exec
  • 原文地址:https://www.cnblogs.com/lindping/p/2004840.html
Copyright © 2020-2023  润新知