PoshCode Archive  Artifact Content

Artifact 5da32ee8c7a249f0e30f4ec41b65f33bf586c837fb1a45a3e577d188c8804ca9:

  • File Shuffle-String.ps1 — part of check-in [dec79bbf82] at 2018-06-10 13:42:36 on branch trunk — Returns a shuffled / randomised / randomized version of an input string (user: David Johnson size: 1438)

# encoding: ascii
# api: powershell
# title: Shuffle-String
# description: Returns a shuffled / randomised / randomized version of an input string
# version: 0.1
# type: function
# author: David Johnson
# license: CC0
# function: Shuffle-String
# x-poshcode-id: 4531
# x-archived: 2013-10-21T07:30:29
# x-published: 2013-10-18T18:04:00
#
# Options available to remove whitespace / CRLF in the returned string.
#
Function Shuffle-String ([string]$String, [switch]$IgnoreSpaces, [switch]$IgnoreCRLF, [switch]$IgnoreWhitespace) {
##
## Simple enough, input a string or here-string, return it randomly shuffled, whitespace, carriage returns and all
## -IgnoreSpaces removes spaces from output
## -IgnoreCRLF removes Carriage Returns and LineFeeds from the output
## -IgnoreWhitespace removes spaces and tabs from the output


    If ($string.length -eq 0) 
    {
        Return
    }
    
    #Tab = [char]9
    
    If ($IgnoreWhiteSpace)
    {
        $String = $String.Replace([string][char]9,"")
        $IgnoreSpaces = $True
    }

    If ($IgnoreSpaces)
    {
        $String = $String.Replace(" ","")
    }

    #CR = [char]13
    #LF = [char]10

    If ($IgnoreCRLF)
    {
        $String = $String.Replace([string][char]10,"").Replace([string][char]13,"")
    }

    $Random = New-Object Random
    
    Return [string]::join("",($String.ToCharArray()|sort {$Random.Next()}))

}