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

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Windows/...', 1758796847, '216.73.216.41', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Windows/PowerShell/language/operator/string-manipulation/index(97): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78