随机字符串C# [英] Shuffle string c#

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

问题描述

我想知道随机字符串

示例字符串

 串字;

//我想将它洗
字=你好
 

我将能够获得:

 兰特==ohlel
兰特==lleho
等等
 

解决方案

C#:

 字符串str =你好;

//随机数序列
随机数,=新的随机();

//从重新排序的字符数组创建新字符串
字符串兰特=新的字符串(str.ToCharArray()。
                排序依据(S =>(num.Next(2)%2)== 0).ToArray());
 

I want to know shuffle string

Example string

string word;

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

I would be able to get:

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

解决方案

C#:

string str = "hello";

// The random number sequence
Random num = new Random();

// Create new string from the reordered char array
string rand = new string(str.ToCharArray().
                OrderBy(s => (num.Next(2) % 2) == 0).ToArray());

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

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