随机字符串 [英] Randomizing a string

查看:162
本文介绍了随机字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C#,所以不要怪我为我的愚蠢。
我的工作应该随机单词,并给它lenght比如你写一个字行的应用程序,它给你艾朗。
目前,我敢肯定,只有这部分作品:

I'm new to C# so don't blame me for my stupidity. I'm working on an application that should randomize a word and give it's lenght for example you write a word "Line" and it gives you "iLen". Currently I'm sure that only this part works:

    private void lenght_Click(object sender, EventArgs e)
    {
        String word = textBox1.Text;
        int x = word.Length;
        MessageBox.Show(x.ToString());
    }

    private void randomize_Click(object sender, EventArgs e)
    {
        String word = textBox1.Text;
        int x = word.Length;


    }



我尝试了很多,但大部分刚刚崩溃的应用程序,这样的时刻我
想知道什么是Text.ToCharArray的确,我很想更多的支持。
所以我只需要一个把你的字符串会随机它给你另一个字符串只是捣碎/随机字符的方法。
现在我将离开5的问题 - 7小时去得到更多的答案后,我将审查所有这些,给推销员的工作的。谢谢你的支持!
我看过你所有的点评,我为迟到抱歉,现在是时候来检查一切。

I tried a lot but most of it just crashed the application so at the moment I would like to know what does the Text.ToCharArray does and I would love additional support. So I just need a method that takes your string randomizes it gives you another string just with mashed/randomized characters. Now I will leave the question for 5 - 7 hours to get more answers later I will review them all and give rep to the working ones. Thank you for support! I have read all your reviews yet I'm late sorry for that now it's time to check everything.

推荐答案

随机化方法(从的其他SO质疑):

public static T[] Randomize<T>(T[] source)
{
    List<T> randomized = new List<T>();
    List<T> original = new List<T>(source);
    Random r = new Random();
    for (int size = original.Count; size > 0; size--)
    {
        int index = r.Next(size);
        randomized.Add(original[index]);
        original[index] = original[size - 1];
    }
    return randomized.ToArray();
}

和用法:

string text = "Line";

string randomized = new string(Randomize(text.ToCharArray()));

这篇关于随机字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆