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

时间:2023-02-06 00:35:36

I have a string, which I have split using the code $CreateDT.Split(" ") I know want to manipulate two separate strings in different ways. How can I separate these into two variables?

我有一个字符串,我使用代码$ CreateDT.Split(“”)拆分我知道想要以不同的方式操作两个单独的字符串。我如何将这些变量分成两个变量?

4 个解决方案

#1


69  

Like this?

喜欢这个?

$string = 'FirstPart SecondPart'
$a,$b = $string.split(' ')
$a
$b

#2


26  

An array is created with the -split operator. Like so,

使用-split运算符创建数组。像这样,

$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

#3


9  

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:

从这里你可以看到string.split()方法:

  • performs a case sensitive split (note that "ALL RIGHT" his split on the "R" but "broken" is not split on the "r")
  • 执行区分大小写的拆分(注意“ALL RIGHT”他在“R”上的拆分​​但“破坏”不会在“r”上拆分)
  • treats the string as a list of possible characters to split on
  • 将字符串视为要拆分的可能字符列表

While the -split operator:

而-split运算符:

  • performs a case-insensitive comparison
  • 执行不区分大小写的比较
  • only splits on the whole string
  • 只分裂整个字符串

#4


3  

try this:

尝试这个:

$Object = 'FirstPart SecondPart' | ConvertFrom-String -PropertyNames Val1, Val2
$Object.Val1
$Object.Val2

#1


69  

Like this?

喜欢这个?

$string = 'FirstPart SecondPart'
$a,$b = $string.split(' ')
$a
$b

#2


26  

An array is created with the -split operator. Like so,

使用-split运算符创建数组。像这样,

$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

#3


9  

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:

从这里你可以看到string.split()方法:

  • performs a case sensitive split (note that "ALL RIGHT" his split on the "R" but "broken" is not split on the "r")
  • 执行区分大小写的拆分(注意“ALL RIGHT”他在“R”上的拆分​​但“破坏”不会在“r”上拆分)
  • treats the string as a list of possible characters to split on
  • 将字符串视为要拆分的可能字符列表

While the -split operator:

而-split运算符:

  • performs a case-insensitive comparison
  • 执行不区分大小写的比较
  • only splits on the whole string
  • 只分裂整个字符串

#4


3  

try this:

尝试这个:

$Object = 'FirstPart SecondPart' | ConvertFrom-String -PropertyNames Val1, Val2
$Object.Val1
$Object.Val2