Bhasker Vengala
Bhasker Vengala

Reputation: 103

get first two digits by using split method in powershell

I want to split one output. the output is

02|05|002|004|0014|0035|R

I tried with

$state.ToString().Split("|")[0]

i got the result like System.Object[]

i want to split the output and assigning to variables like

$a='02'
$b='05'

please help me to complete this

Upvotes: 2

Views: 7253

Answers (2)

Shay Levy
Shay Levy

Reputation: 126772

Here's a simplified solution that uses the range operator to return the first two elements and assign them to variables:

$a,$b = '02|05|002|004|0014|0035|R'.Split('|')[0..1]

Upvotes: 3

vasja
vasja

Reputation: 4792

Put them to the array using select -first

$state = '02|05|002|004|0014|0035|R'
$list = @()
$list = $state.ToString().Split("|") | select -First 2
[string] $a = $list[0]
[string] $b = $list[1]
write-host $a
write-host $b

Upvotes: 1

Related Questions