PowerShell: Split String
💡 TIP: Operator names are case-insensitive.
Operator: -SPLIT
str -Split delimiter
-
Return a Array.
- delimiter is a string, can be multiple characters.
- by default, delimiter is case-insensitive.
- by default, delimiter is interpreted as Regular Expression.
- The delimiters are removed from string. To include them in string, add parenthesis around it. e.g.
"(,)"
splits string by comma, but include it in the result array.
"a,b,c" -Split "," # return array of a b c
# split, but include the separator "a,b,c" -Split "(,)" # return array, of 5 elements
str -Split delimiter, max
-
- Split up to max count. Rest of string is concatenated as last element in array.
- max value of 0 means return all.
- A negative max gets strings from the right.
when including delimiter in result, the delimiter does not count towards max count.
$x = "a b c" -Split " " , 1 Write-Host (1 -eq $x.length) # True $x = "a b c" -Split " " , 2 Write-Host (2 -eq $x.length) # True
str -Split delimiter, max, options
-
Options is a comma separated string, of one of the following
- combination of
SimpleMatch
IgnoreCase
- combination of
RegexMatch
IgnoreCase
CultureInvariant
IgnorePatternWhitespace
ExplicitCapture
Singleline
Multiline
# example of ignore case $x = "1A2A3" -Split "a", 0 , "IgnoreCase" Write-Host $x # 1 2 3
# example of not using regex $x = "1.2.3" -Split ".", 0 , "SimpleMatch" Write-Host $x # 1 2 3
str -Split {ScriptBlock}
-
Use a script block.
str -Split {ScriptBlock}, Max
-
Use a script block with Max
-iSplit
-
Case-insensitive. Same as
-Split
. -cSplit
-
Case-sensitive.
-Split str
-
Split string by whitespace such as space, tab, line return char.
Return a Array .
-split "cat dog"
-Split (str1, str2, etc)
-
Split multiple strings. Return a single array.
$x = -Split ("a b", "c d")
PowerShell String
- PowerShell: Quote String
- PowerShell: Double Quoted String, String Expansion
- PowerShell: Here-String
- PowerShell: Escape Characters
- PowerShell: String Operators
- PowerShell: Join String
- PowerShell: Split String
- PowerShell: Format String
- PowerShell: String Methods
- PowerShell: String Wildcards
- PowerShell: Regular Expression Operators
- PowerShell: Regular Expression Syntax