Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Shuffle the characters in a string but excluding the original string that was passed in. The method checks that we have passed in a string of more than two characters as if we have less than two characters it is not possible to return anything but the original string.
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",
        shuffled = new string(
                                    .OrderBy(character => Guid.NewGuid())
    } while (shuffled == stringToShuffle);
    return shuffled;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment