Split and join operators

The -split and -join operators are very powerful tools that you should add to your repertoire. You can easily split complex strings into sub-parts and combine them with the join operator:

Operator
Meaning
Description
 -split

Split string

Splits strings and returns values as arrays.

 -isplit

Split string—explicitly case-insensitive

Splits strings and returns values as arrays - explicitly case-insensitive.

 -csplit

Split string - case-sensitive

Splits strings and returns values as arrays - case-sensitive.

 -join

Join strings

Joins strings with specific layouts together.

 

First, we will take a look at the split operator. Here, you can additionally add the leading letters for case-sensitivity and case-insensitivity (-csplit and -isplit). The usage of the split operator can be executed in the following three ways:

  • -Split <String>
  • <String> -Split <Delimiter>[,<Max-substrings>[,"<Options>"]]
  • <String> -Split {<ScriptBlock>} [,<Max-substrings>]

<String> specifies one or more strings that you want to split into parts. The easiest way to use it is as follows:

# simple usage of -split
-split "PowerShell makes fun." # 'PowerShell', 'makes', 'fun.'

In addition, you can use your own delimiter for separation:

# using your own delimiter 
'ID:Name' -split ':' # 'ID', 'Name'

By default, the delimiter is omitted in the results. If you want the delimiter to be preserved, you can put the delimiter in parentheses, as follows:

# using your own delimiter - preserving delimiter
'ID:Name' -split '(:)' # 'ID', ':' ,'Name'

# using your own delimiter - preserving delimiter - omitting specific characters
'ID/:/Name' -split '/(:)/' # 'ID', ':' ,'Name'

You can also define the maximum number of sub-strings that will be returned:

# using your own delimiter with 3 maximum substrings
'ID:Name:Street:City' -split ':',3 # 'ID', 'Name', 'Street:City'

As a replacement for a defined delimiter, it is also possible to use scriptblock. Every time scriptblock returns $true, these characters are used as delimiters:

# using a scriptblock
'ID:Name:Street:City' -split {($_ -eq 'a') -or ($_ -eq 't') } # 'ID:N', 'me:S', 'ree', ':Ci', 'y'

For most examples, this knowledge will help with daily use.

For further examples and more comprehensive information, you can take a dedicated look at the Help with Get-Help about_Split.

Next, we will take a look at the join operator to join strings together with defined characters. There are two ways to use it:

  • -Join <String[]>
  •  <String[]> -Join <Delimiter>

Take a look at the following example to understand the syntax:

# simple example for joining strings
-join 'P', 'ower', 'Shell' # 'P', 'ower', 'Shell' # not working as planned
-join ('P', 'ower', 'Shell') # 'PowerShell'

$
strings = 'P', 'ower', 'Shell'
-join $strings # 'PowerShell'

You can also specify the delimiters for joining the strings together:

# joining with a specified delimiter
('This', 'is', 'PowerShell.') -join ' ' # This is PowerShell.
'This', 'is', 'PowerShell.' -join ' ' # This is PowerShell.
'ID', 'USER', 'NAME' -join '|' # ID|USER|NAME

For learning purposes, you can try to combine the -split and -join operators. You will need these two operators very frequently.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset