c# - 随机字符串 c#

标签 c# algorithm ironpython

我想知道随机串

示例字符串

string word;

//I want to shuffle it
word = "hello"  

我将能够得到:

rand == "ohlel"
rand == "lleho"
etc.

最佳答案

这个解决方案(以扩展方法的形式)很好:

    public static string  Shuffle(this string str)
    {
        char[] array = str.ToCharArray();
        Random rng = new Random();
        int n = array.Length;
        while (n > 1)
        {
            n--;
            int k = rng.Next(n + 1);
            var value = array[k];
            array[k] = array[n];
            array[n] = value;
        }
        return new string(array);
    }

关于c# - 随机字符串 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4739903/

相关文章:

.net - 查询托管环境中接口(interface)的 IronPython 脚本

algorithm - 确定这些循环的 Big-O 运行时

algorithm - 最大限度地降低转型成本

c++ - 使用对象名称识别容器中的对象?

c# - RegistrySecurity 访问被拒绝。 C#

ironpython - Spotfire IronPython - 从数据表计算出的值?

clr - 编写Iron Python调试器

java - 使用 OpenCV 验证申请表

c# - 如何确定哪个鼠标按钮引发了 WPF 中的单击事件?

时间:2019-03-17 标签:c#WebAPIlocalization