Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- internal class ConsoleTests
- {
- public static string ShuffleString(string stringToShuffle)
- {
- if (String.IsNullOrEmpty(stringToShuffle))
- {
- throw new ArgumentNullException("stringToShuffle",
- "The stringToShuffle variable must not be null or empty");
- }
- return new string(
- stringToShuffle
- .OrderBy(character => Guid.NewGuid())
- .ToArray()
- );
- }
- public static string ShuffleStringExcludeOriginal(string stringToShuffle)
- {
- string shuffled;
- if (String.IsNullOrEmpty(stringToShuffle))
- {
- throw new ArgumentNullException("stringToShuffle",
- "The stringToShuffle variable must not be null or empty");
- }
- if (stringToShuffle.Length < 2)
- {
- throw new ArgumentException("This method can not be used when the string to shuffle is less than two " +
- "characters long as it is not possible to exclude the original",
- "stringToShuffle");
- }
- do
- {
- shuffled = new string(
- stringToShuffle
- .OrderBy(character => Guid.NewGuid())
- .ToArray()
- );
- } while (shuffled == stringToShuffle);
- return shuffled;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment