Table of Contents
Using Split()
Method
Use the Split()
method to split the given string into two variables.
1 2 3 4 5 6 |
$string = "This is a sample string" $var1_string, $var2_string = $string.Split(" ", 2) $var1_string $var2_string |
1 2 3 4 |
This is a sample string |
The Split() method is used to split the specified string into two/multiple variables and an array of substrings. In the above example code, the Split()
method took two arguments, whitespace as a separator (you can use -
, .
, ,
, :
, or anything else) and 2
as the maximum number of substrings after the split.
This method splits the $string
where it finds the first occurrence of the specified separator (whitespace in this case) and stores the first substring in $var1_string
and the second substring in $var2_string
variables. Similarly, we can use this method to split $string
into multiple variables.
Use the Split()
method to split the given string into multiple variables.
1 2 3 4 5 6 7 |
$string = "This is a sample string" $var1_string, $var2_string, $var3_string = $string.Split(" ", 3) $var1_string $var2_string $var3_string |
1 2 3 4 5 |
This is a sample string |
For this example, the Split()
method took whitespace as a separator and 3
as the maximum number of substrings after splitting. Here, the $string
will be split at the first and second occurrence of the specified separator and save the first, second, and third substrings in the $var1_string
, $var2_string
, and $var3_string
variables. Note that all these three variables are of the String
data type. You can chain the GetType()
method as $var2_string.GetType()
to retrieve the data type of the specified variable.
Use the Split()
method to split the given string into an array of substrings.
1 2 3 4 5 |
$string = "This is a sample string" $Array = $string.Split(" ") $Array |
1 2 3 4 5 6 7 |
This is a sample string |
In this code block, the Split()
method accepted only one argument: a separator and split the $string
wherever it found the match. In this case, the Split()
method returned an array of substrings. Note that we can use $string.Split()
instead of $string.Split(" ")
, both are equivalent because the default separator is the whitespace (tab/newline/space).
Remember, the Split()
method is case-sensitive, meaning the letters S
and s
will differ. See the following examples to understand.
1 2 3 4 |
$string = "This is a Sample string" $string.Split("S", 2) |
1 2 3 4 |
This is a ample string |
As we can see, the Split()
method only splits the $string
where it finds the uppercase S
letter and ignores the lowercase s
. Therefore, if we are required to ignore lower/upper case letters, we must use the -Split
operator, demonstrated with code examples in the following section.
The Split() method always returns an array of substrings. It depends on us how we deal with them. For instance, if we are splitting the string and saving substrings into two or more variables, then each variable will contain a string type value. On the other hand, if we save the resulting substrings into one variable, it would be treated as an array of string values.
Using -Split
Operator
Use the -Split
operator to split the given string into two variables.
1 2 3 4 5 6 |
$string = "This is a sample string" $var1_string, $var2_string = $string -Split " ", 2 $var1_string $var2_string |
1 2 3 4 |
This is a sample string |
Here, we used the -Split
operator with two arguments, a separator and the maximum number of substrings, to split the specified string into two variables. It works like the Split()
method that we have learned in the previous section but allows us to do a few additional things; for instance, we can use script block, specify conditions using options such as IgnoreCase
, etc. that we will learn in a while in this article.
Use the -Split
operator to split the given string into multiple variables.
1 2 3 4 5 6 7 |
$string = "This is a sample string" $var1_string, $var2_string, $var3_string = $string -Split " ", 3 $var1_string $var2_string $var3_string |
1 2 3 4 5 |
This is a sample string |
This code is similar to the previous example, but we split the $string
into three variables where each variable ($var1_string
, $var2_string
, and $var3_string
) is of String
data type.
Use the -Split
operator to split the given string into an array of substrings.
1 2 3 4 5 |
$string = "This is a sample string" $Array = $string -Split " " $Array |
1 2 3 4 5 6 7 |
This is a sample string |
For the above code, we used the -Split
operator to split the whole $string
based on the specified separator (delimiter). We can also substitute -cSplit
or -iSplit
for the -Split
operator in any binary split statement (a split statement which includes a script block or a delimiter).
Here, the -Split
and -iSplit
are case-insensitive while -cSplit
is case-sensitive, which means the case would be considered while applying the delimiter rules (separator rules). See the following example.
1 2 3 4 5 6 7 |
$string = "This is a Sample string" $var1, $var2 = $string -Split "s", 2 $var1, $var2 $var1, $var2 = $string -iSplit "S", 2 $var1, $var2 |
1 2 3 4 5 6 |
Thi is a Sample string Thi is a Sample string |
See, we got the same results for using -Split
and -iSplit
because both are case-insensitive operators, while -cSplit
takes care of lower/upper case while applying separator rules. See the following example.
1 2 3 4 5 6 7 |
$string = "This is a Sample string" $var1, $var2 = $string -cSplit "s", 2 $var1, $var2 $var1, $var2 = $string -cSplit "S", 2 $var1, $var2 |
1 2 3 4 5 6 |
Thi is a Sample string This is a ample string |
Here, we used -cSplit
twice. First, it split when it found s
; second, it split when it found S
. Suppose a situation where you are bound to use the -cSplit
operator but have to ignore the case. In that situation, Options
come into the picture. See the following example to ignore the upper/lower case while using the -cSplit
operator.
1 2 3 4 5 6 7 |
$string = "This is a Sample string" $var1, $var2 = $string -cSplit "s", 2, "IgnoreCase" $var1, $var2 $var1, $var2 = $string -cSplit "S", 2, "IgnoreCase" $var1, $var2 |
1 2 3 4 5 6 |
Thi is a Sample string Thi is a Sample string |
Here, the -cSplit
does not care about the upper/lower case when used with the IgnoreCase
option, which is part of SimpleMatch
. Now, do we have other options as well? Yes, we do. The SimpleMatch
options include IgnoreCase
and SimpleMatch
.
While the RegexMatch
options include IgnoreCase
, RegexMatch
, ExplicitCapture
, CultureInvariant
, IgnorePatternWhitespace
, Multiline
, and Singleline
, you can find a detailed description for each of them here.
Use the -Split
operator with the script block to split the given string into an array of substrings.
1 2 3 4 5 |
$string = "This is a Sample string" $Array = $string -Split {$_ -eq "s" -or $_ -eq "p"} $Array |
1 2 3 4 5 6 7 8 |
Thi i a am le tring |
Here, we used a script block represented with {}
, which specified two conditions, and checked the current character whether is equal to s
or p
. We used the -eq
operator to check equality while the -or
operator performed the OR
operation. The OR
operation results in true
if at least one condition is true
. Note that the -Split
operator only splits the $string
if the script block returns true
.
Using ForEach
Loop
Use the ForEach
loop to split the given string into an array of substrings.
1 2 3 4 5 |
$string = "This is a Sample string" $Array = $string | foreach split " " $Array |
1 2 3 4 5 6 7 |
This is a Sample string |
Here, we used the ForEach
loop (also referred to as ForEach
statement) to iterate over the String
type object, which is $string
. Each iteration splits the string where the specified delimiter was found and returns an array of substrings.
That’s all about how to split String into Variables in PowerShell.