TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

PowerShell Split and Join Operators

PowerShell Split and Join Operators with What is PowerShell, History of PowerShell, Features of PowerShell, PowerShell vs CMD, PowerShell Scripting, PowerShell Versions, PowerShell Commands, PowerShell Looping, PowerShell Conditions, PowerShell Array, PowerShell Brackets etc.

<< Back to POWERSHELL

Split and Join Operators

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:

  1. <string> : This parameter is used to specify the one or more string to be split. The same delimiter rule splits the multiple strings.
  2. <delimiter> : The default delimiter is " ". When the strings are split, it is omitted from all the substrings.
  3. <max-substrings> : This parameter is used to specify the maximum number of times that a string splits.
  4. <ScriptBlock> : This parameter is an expression that specifies the rules for applying the delimiter. We must enclose the script block in the braces"{}".
  5. <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

Next TopicIf Statement




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf