繁体   English   中英

将字符串拆分为单独的变量

[英]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()方法

  • 执行区分大小写的拆分(注意“ALL RIGHT”他在“R”上的拆分​​但“broken”不会在“r”上拆分)
  • 将字符串视为要拆分的可能字符列表

-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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM