PowerShell: String Operators
Operator names are case-insensitive.
String Length
"abc".length
Join String
Join String +
string can be joined by plus sign +
.
# join string $x = "a" + "b"
"a"+"b"+"c" # "abc"
-Join operator
-Join (str1, str2 etc)
-
Join strings. (Parenthesis required.)
-join ("a", "b", "c") # "abc"
(str1, str2 etc) -Join Delimiter
-
Join strings with delimiter. delimiter is a string and can have multiple characters.
("a", "b", "c") -join " " # "a b c"
Join-String cmdlet
There is also a Join-String
cmdlet.
-Split Operator
-Split str
- Split string by whitespace such as
`n
,`t
. Return a array.-split "cat dog"
-Split (str1, str2 etc)
- Can have multiple operands.
str -Split delimiter
- Use a delimiter.
• delimiter is a string, can be multiple characters.
• by default, delimiter is case-insensitive.
• by default, delimiter is interpreted as regex.
• The delimiters are removed from string. To include them in string, add parenthesis around it. For example:"(,)"
splits string by comma, but include it in the result array. str -Split delimiter, max
- Split up to max count. Rest of string is concatenated as last element in array. Note, max is not the result length. Also, when including delimiter in result, it does not count towards max count.
str -Split delimiter, max, options
-
Options is one of:
"SimpleMatch"
,"SimpleMatch,IgnoreCase"
- Comma separated string of one or more of
RegexMatch
,IgnoreCase
,CultureInvariant
,IgnorePatternWhitespace
,ExplicitCapture
- Comma separated string of one of
Singleline
,Multiline
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.
String Match by Wildcards ( -like operator)
-like
-
String matches wildcard pattern. Case-insensitive
"mycat.jpg" -like "*jpg" # True # the wildcard must be on right side
-iLike
-
Case-insensitive. Same as
-Like
. -cLike
- Case-sensitive
-notLike
- Negation.
-iNotLike
-
Case-insensitive. Same as
-NotLike
. -cNotLike
- Case-sensitive