我只是想知道是否有一种方法(使用ASP.NET C#)“混乱”字符串的内容,但仍然能够点击另一个按钮并将其“重新混合”回原始内容而不保存原创内容?
谢谢:)
示例:
"This is not shuffled."
"isuo .tffsnl iTh shed"
...And then I click the "UNShuffle" button and it becomes normal again:
"This is not shuffled."
答案 0 :(得分:8)
这很简单:
var rnd = new Random();
string unsuffled = "This is not shuffled.";
string shuffled = new string(unsuffled.OrderBy(r => rnd.Next()).ToArray());
但是因为它是随机的,所以除非你存储前一个字符串或映射,否则你不能解开。
答案 1 :(得分:6)
嗯,你需要保存某些东西。一个简单的想法:
Random
的新实例以用于使用该种子进行混洗随后洗牌是可逆的 - 诚然需要付出一点努力。 (我可能会以相同的方式将数字0 ...(n-1)洗牌,然后以相反的方式反向映射字符。)
棘手的一点是你做需要种子 - 它有点像存储密码哈希中的盐。你必须得到一些额外的信息来说明它是如何改组的,否则你不会知道“abc”是来自“bac”还是“cab”。