Search notes:

PowerShell: String manipulation operators

PowerShell operators for string manipulation are -join, -split, -replace

-replace

-replace replaces a (sub-string) of a string or a string array with another string. Regular expressions are possible.
#  Replace in ONE string
#
'foo, bar and baz' -replace 'ba', 'xyz'
#
# foo, xyzr and xyzz

#  Replace in collection of strings
#
'foo', 'bar', 'and', 'baz' -replace 'ba', 'xyz'
#
# foo
# xyzr
# and
# xyzz

#  Replace with regular expressions.
#  Two adjacent equal letters are replaced
#  with the number 2 and the letter:
#
'apple', 'pear', 'hello' -replace '(.)\1', '2$1' -join ', '
#
# a2ple, pear, he2lo

Github repository about-PowerShell, path: /language/operator/string-manipulation/replace.ps1

-split / -join

-split creates an array from a string, -join creates a string from an array.
$csv_line = 'foo;bar;baz;etc'

$elems = $csv_line -split ';'

$elems
#
# foo
# bar
# baz
# etc
#


$joined = $elems -join '*'

$joined
#
# foo*bar*baz*etc
#
Github repository about-PowerShell, path: /language/operator/string-manipulation/split-join.ps1
-split and -join behave very much like the Perl functions split() and join().
Note that the -join operator can also be used to concatenate strings.

Index