functions/string/Split-PSUString.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
function Split-PSUString
{
<#
    .SYNOPSIS
        Splits a string. In a pipeline.
     
    .DESCRIPTION
        Splits a string. In a pipeline.
     
    .PARAMETER InputString
        The string(s) to split
     
    .PARAMETER With
        Default: "`n"
        What to split the string with
     
    .PARAMETER Simple
        Whether to disable regex when splitting.
     
    .PARAMETER Options
        Regex options to consider
     
    .EXAMPLE
        "abc,def" | split ","
     
        Returns "abc","def"
#>

    [CmdletBinding(DefaultParameterSetName = "Regex")]
    Param (
        [Parameter(ValueFromPipeline = $true)]
        [string[]]
        $InputString,
        
        [Parameter(Position = 0)]
        [string]
        $With = "`n",
        
        [Alias('r')]
        [Parameter(ParameterSetName = "Simple")]
        [switch]
        $Simple,
        
        [Parameter(ParameterSetName = "Regex")]
        [System.Text.RegularExpressions.RegexOptions]
        $Options = [System.Text.RegularExpressions.RegexOptions]::IgnoreCase
    )
    
    begin
    {
        $IsRegex = $PSCmdlet.ParameterSetName -eq "Regex"
    }
    process
    {
        foreach ($line in $InputString)
        {
            if ($IsRegex) { [regex]::Split($line, $With, $Options) }
            else { $line.Split($With) }
        }
    }
    end { }
}
Import-PSUAlias -Name split -Command Split-PSUString