I have a string, which I have split using the code $CreateDT.Split(" ")
. I now want to manipulate two separate strings in different ways. How can I separate these into two variables?
Asked
Active
Viewed 3.5e+01k times
89

Peter Mortensen
- 30,738
- 21
- 105
- 131

davetherave
- 1,147
- 3
- 10
- 15
-
See similar http://stackoverflow.com/questions/11348506/split-string-with-powershell-and-do-something-with-each-token – Michael Freidgeim Jan 14 '17 at 12:25
6 Answers
149
Like this?
$string = 'FirstPart SecondPart'
$a,$b = $string.split(' ')
$a
$b

Esperento57
- 16,521
- 3
- 39
- 45

mjolinor
- 66,130
- 7
- 114
- 135
-
9If you have `"FirstPart SecondPart ThirdPart"` and want only two, you can use `$a,$b = $string.split(' ')[0,1]` – Stoinov May 29 '19 at 18:03
-
If you have $string = 'FirstPart - SecondPart' and $a,$b = $string.split('-'), add ..Trim() ($a,$b = $string.split('-').Trim()), then both $a and $b is trimmed – mortenma71 Apr 02 '20 at 04:58
56
An array is created with the -split
operator. Like so,
$myString="Four score and seven years ago"
$arr = $myString -split ' '
$arr # Print output
Four
score
and
seven
years
ago
When you need a certain item, use array index to reach it. Mind that index starts from zero. Like so,
$arr[2] # 3rd element
and
$arr[4] # 5th element
years

vonPryz
- 22,996
- 7
- 54
- 65
-
6
-
2Also can be a one-liner: `$first_word = ("one-two-three" -split '-')[0]` – information_interchange Jan 17 '18 at 18:36
-
3@sxc731: `.Split()` always returns an array, so there's no need for `@(...)`: `$arr = $myString.split(' ')`, but note that the more PowerShell-idiomatic solution, `$arr = $myString -split ' '`, is already part of the answer. Do note, however, that `-split` takes a _regex_ and is case-insensitive (use `-csplit` for case-sensitive splitting). – mklement0 Oct 17 '18 at 21:47
26
It is important to note the following difference between the two techniques:
$Str="This is the<BR />source string<BR />ALL RIGHT"
$Str.Split("<BR />")
This
is
the
(multiple blank lines)
source
string
(multiple blank lines)
ALL
IGHT
$Str -Split("<BR />")
This is the
source string
ALL RIGHT
From this you can see that the string.split()
method:
- performs a case sensitive split (note that "ALL RIGHT" his split on the "R" but "broken" is not split on the "r")
- treats the string as a list of possible characters to split on
While the -split
operator:
- performs a case-insensitive comparison
- only splits on the whole string
-
4These are good differences to point out; another important one is that `-split` takes a _regex_ (regular expression) as its (first) RHS operand, whereas the `[string]` type's `.Split()` method operates on a _literal_ character / array of characters / and - in .NET _Core_ - also a literal _string_. Also, note that the proper syntax in PowerShell is `$Str -split '
'`; while pseudo-method syntax `$Str -Split("
")` _happens to_ work in this case, it is to be avoided. – mklement0 Oct 17 '18 at 21:36
9
Try this:
$Object = 'FirstPart SecondPart' | ConvertFrom-String -PropertyNames Val1, Val2
$Object.Val1
$Object.Val2

Peter Mortensen
- 30,738
- 21
- 105
- 131

Esperento57
- 16,521
- 3
- 39
- 45
-
ConvertFrom-String is the better solution. It should be noted that ConvertFrom-String is actually designed with specific intent of splitting and objectifying strings of information, which is exactly what OP desires. It should also be noted that while the split commandlet works, it takes additional manipulation to get what OP desires, something ConvertFrom-String takes care of. – danno Oct 02 '18 at 15:34
-
2@danstermeister: Generally, using a _cmdlet_ for simple string splitting is inefficient. Specifically, `ConvertFrom-String` is obsolescent and should be avoided (it always had an experimental feel and the fact that it hasn't been included in PowerShell Core, where future development efforts are focused, indicates that it's not here to stay). As for the need for additional manipulation: a destructuring assignment will do: `$val1, $val2 = 'FirstPart SecondPart' -split ' '` – mklement0 Oct 17 '18 at 21:30
-
By default you can do it : $Object = 'FirstPart SecondPart' | ConvertFrom-Csv -d ' ' -H Val1, Val2 (same with Core 6) ;) – Esperento57 Oct 18 '18 at 06:04
-
1@mklement0 - PowerShell devs didn't import all windows-centric items so far due to the backend work necessary, but are slowing achieving that for actual feature-parity. So unless it was specifically deprecated in windows powershell I wouldn't necessarily count it out. The netadapter commands, for instance, I believe will be ported in the near-future, but don't exist today. It's mere absence currently in Core is absolutely no predictor of it's existence in Core in the future. And cmdlet efficiency concerns are better left to actual devs, not drive-by scripters. – danno Oct 25 '18 at 16:29
-
@danstermeister: True, in the absence of further information, you cannot infer that it _won't_ be ported from the current absence - only that it wasn't a _priority_. That is a moot point, however, because all of `ConvertFrom-String`'s _inherent_ problems are reason enough to avoid it. This especially applies to the template-based parsing, but even the delimiter-based parsing is flawed, due to _type inference_ that is _invariably_ applied - see https://stackoverflow.com/a/50166580/45375. – mklement0 Oct 25 '18 at 17:07
-
@danstermeister: As for alternatives: As stated, in _expressions_ such as here `-split` is the superior alternative: `-split 'FirstPart SecondPart'`, in the simplest form - no expensive pipeline and no construction of unnecessary custom objects. As for _pipeline use_: Watch for the [upcoming string-manipulation cmdlets](https://github.com/PowerShell/PowerShell-RFC/blob/2ddf2230f63cd100177a504509c27d0c9c23265e/1-Draft/RFCNNNN-String-Manipulating-Cmdlets.md). – mklement0 Oct 25 '18 at 17:09
0
Foreach-object operation statement:
$a,$b = 'hi.there' | foreach split .
$a,$b
hi
there

js2010
- 23,033
- 6
- 64
- 66
0
Incase, we want to split a string into separate characters, then we can use .ToCharArray()
$Inputstring ="Hello World"
$Array =$InputString.ToCharArray()
$Array
Output:
H
e
l
l
o
W
o
r
l
d
You can take a look here: https://qawithexperts.com/article/technology/split-string-into-array-in-powershell/519

neena
- 71
- 4