The Split and Join operators are used in PowerShell to divide and combine the substrings.
-Join Operator
The -Join operator is used in PowerShell to combine the set of strings into a single string. The strings are combined in the same order in which they appear in the command.
The following two statements are the syntax to use the Join operator:
- -Join <String>
- <String> -Join <Delimiter>
In the above syntax, the <String> parameter is used to specify the one or more strings to be joined. And the <Delimiter> parameter is used to specify one or more characters placed between the combined strings. The default Delimiter is ” “.
Note: If we use the unary join operator (-join <String>) to combine the strings, we must enclose them in parenthesis, or store them in a variable.
Examples: The below examples describe how to use the unary and binary (With Delimiter) Join operator in different ways:
Example1:
- – Join “windows”,”Operating”,”System”
The command in this example displays the following output:
Windows Operating System
Example2:
- – Join (“windows”,”Operating”,”System”)
The command in this example displays the following output:
WindowsOperatingSystem
Example3:
- $a = “windows”,”Operating”,”System”
- – Join $a
The second command in this example displays the following output:
WindowsOperatingSystem
Example4:
- $x = “WIND”, “S P”, “ERSHELL”
- $x -join “OW”
This example uses the multiple-character delimiter to join the three strings, which are stored in the variable $x. The second command in this example displays the following output:
WINDOWS POWERSHELL
-Split Operator
The -Split operator is used in PowerShell to divide the one or more strings into the substrings.
The following statements are the syntax to use the -split operator:
- -Split <String>
- -Split (<String[]>)
- <String> -Split <Delimiter>[,<Max-substrings>[,”<Options>”]]
- <String> -Split {<ScriptBlock>} [,<Max-substrings>]
In the above Syntax, the following parameters are used:
- <string> : This parameter is used to specify the one or more string to be split. The same delimiter rule splits the multiple strings.
- <delimiter> : The default delimiter is ” “. When the strings are split, it is omitted from all the substrings.
- <max-substrings> : This parameter is used to specify the maximum number of times that a string splits.
- <ScriptBlock> : This parameter is an expression that specifies the rules for applying the delimiter. We must enclose the script block in the braces”{}”.
- <Options> : This parameter is valid only when the <max-substring> parameter is used in the statement.
Examples: The following examples describe how to use the -split operator in different ways:
Example 1:
- -split “a b c d e f g h”
This command displays the following output:
a
b
c
d
e
f
g
h
Example2:
- $a = “a b c d e f g h”
- -split $a
The output of this example is the same as the output of example1.
Example3:
- $a = “a=b=c=d=e=f=g=h”
- $a -split “=”
The output of this example is also same as the output of example1.
Example4:
- $a = “a=b=c=d=e=f=g=h”
- $a -split “=”,3
This example displays the following output:
a b c=d=e=f=g=h
Example5:
- $a = “a=b=c=d=e=f=g=h”
- $a -split { $_ -eq “b” -or $_ -eq “f”}
This example displays the following output:
a=
=c=d=e=
=g=h
Leave a Reply