[英]Splitting a string into separate variables
我有一个字符串,我使用代码$CreateDT.Split(" ")
将其拆分。 我现在想以不同的方式操作两个单独的字符串。 我怎样才能将这些分成两个变量?
像这样?
$string = 'FirstPart SecondPart'
$a,$b = $string.split(' ')
$a
$b
使用-split
运算符创建数组。 像这样,
$myString="Four score and seven years ago"
$arr = $myString -split ' '
$arr # Print output
Four
score
and
seven
years
ago
当您需要某个项目时,请使用数组索引来访问它。 请注意索引从零开始。 像这样,
$arr[2] # 3rd element
and
$arr[4] # 5th element
years
重要的是要注意这两种技术之间的以下区别:
$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
从这里你可以看到string.split()
方法:
而-split
运算符:
尝试这个:
$Object = 'FirstPart SecondPart' | ConvertFrom-String -PropertyNames Val1, Val2
$Object.Val1
$Object.Val2
Foreach-object操作语句:
$a,$b = 'hi.there' | foreach split .
$a,$b
hi
there
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.