当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 在 PowerShell 中连接字符串 [4 种方法]

作者:精品下载站 日期:2024-12-14 05:24:19 浏览:13 分类:玩电脑

在 PowerShell 中连接字符串 [4 种方法]


使用运算符

要在 PowerShell 中连接字符串:

  • 声明并初始化包含字符串类型值的变量。
  • 使用 +-f-join 运算符连接我们在第一步中创建的所有变量的值。

使用运算符:

$greetings = "Hi"
$first_name = "Mehvish"
$last_name = "Ashiq"

$plus_without_separator = $greetings + $first_name + $last_name
$plus_with_separator = $greetings +" "+ $first_name +" "+ $last_name

$f_without_separator = "{0}{1}{2}" -f $greetings, $first_name, $last_name
$f_with_separator = "{0} {1} {2}" -f $greetings, $first_name, $last_name

$join_without_separator = $greetings, $first_name, $last_name -join "" 
$join_with_separator = $greetings, $first_name, $last_name -join " " 

$props = $([ordered]@{
    plus_without_separator=$plus_without_separator
    plus_with_separator=$plus_with_separator
    f_without_separator=$f_without_separator
    f_with_separator=$f_with_separator
    join_without_separator=$join_without_separator
    join_with_separator=$join_with_separator
    })

$results=@()
$results += New-Object psobject -Property $props
Write-Output $results

输出 :

plus_without_separator : HiMehvishAshiq
plus_with_separator    : Hi Mehvish Ashiq
f_without_separator    : HiMehvishAshiq
f_with_separator       : Hi Mehvish Ashiq
join_without_separator : HiMehvishAshiq
join_with_separator    : Hi Mehvish Ashiq

我们可以使用 +-f-join 运算符在 PowerShell 中连接带/不带分隔符的字符串。下面我们来了解一下这些运算符的用途和区别。

首先,我们声明并初始化了三个变量,$greetings$first_name$last_name,其中包含 Hi、 分别。但是,它与 + 运算符不同,因为它允许我们通过更改变量的位置来将字符串放在特定索引上。

我们可以在每个大括号对之间放置一个空格,以将字符串彼此分开。大括号中的数字是索引。我们可以更改值的顺序而无需重新排列变量,如下所示。

使用 -f 运算符:

$greetings = "Hi"
$first_name = "Mehvish"
$last_name = "Ashiq"

$result = "{2}{1}{0}" -f $greetings, $first_name, $last_name
$result

输出 :

AshiqMehvishHi

我们使用的第三个运算符是 -join 运算符;它也像 +-f 运算符一样工作; -join 运算符的好处是我们不必重复编写分隔符,而是在 -join 参数之后传递一次。

我们将结果保存在 $plus_without_separator$plus_with_separator$f_without_separator$f_with_separator$join_without_separator 中使用 +-f-join 运算符后得到的 $join_with_separator 变量有和没有分隔符。

我们可以在 PowerShell 控制台上单独编写这些变量来显示输出,但我们采用了不同的方法来同时查看所有结果。为此,我们使用 @{} 创建一个哈希表,这是一种具有特定值的唯一键的数据结构;我们用它来拥有键值格式的属性。

接下来,我们将 @{}$ ([ordered]) 括起来,作为 $ ([ordered]@{})按照属性的创建顺序拥有属性;否则,它们将随机显示。请参阅以下输出以获得清晰的图片。

输出 :

join_without_separator : HiMehvishAshiq
join_with_separator    : Hi Mehvish Ashiq
f_without_separator    : HiMehvishAshiq
plus_with_separator    : Hi Mehvish Ashiq
f_with_separator       : Hi Mehvish Ashiq
plus_without_separator : HiMehvishAshiq

此时,我们使用哈希表创建了一个包含我们定义的所有属性的对象,并将其保存在 $props 变量中。如何?让我们看一下。 New-Object 创建了一个对象,而 -Property 参数设置了每个属性值,并按照它们在哈希表中出现的顺序调用了新创建对象的每个方法。

请记住,哈希表包含键值格式的数据,其中键是方法或属性的名称,而值是方法参数或属性值。那么现在的重点是,我们为什么要使用 PSObject 类?

我们使用 PSObject 类,因为如果从 PSObject 类派生一个新对象,并且我们编写了该对象上不存在的属性,则 New -Object 将把我们指定的属性作为 NoteProperty 添加到我们的对象中。因此,对于这种特殊情况,如果我们不使用 PSObject 类,New-Object 命令将产生一个非终止错误。

我们将对象的数据保存在使用数组运算符 (@()) 创建的 $result 数组中,最后,我们使用了 Write-Output cmdlet 在 PowerShell 控制台上打印指定变量的值。

请注意,Write-Output 会将对象发送到名为 Output StreamSuccess Pipeline 的主管道。此 cmdlet 用于在 PowerShell 控制台上显示对象和字符串。例如,如果出现错误,我们将使用 Write-Error 将错误对象发送到错误管道。

使用内置函数

要在 PowerShell 中连接字符串:

  • 声明并初始化三个包含字符串类型值的变量。
  • 使用 concat()Join()Format() 函数连接我们在第一步中创建的所有变量的值。

使用内置函数:

$greetings = "Hi"
$first_name = "Mehvish"
$last_name = "Ashiq"

$concat_without_separator = [String]::Concat($greetings, $first_name, $last_name) 
$concat_with_separator = [String]::Concat($greetings, " ", $first_name, " ", $last_name)

$join_without_separator = [String]::Join("", $greetings, $first_name, $last_name) 
$join_with_separator = [String]::Join(" ", $greetings, $first_name, $last_name)

$format_without_separator = [String]::Format("{0}{1}{2}", $greetings, $first_name, $last_name) 
$format_with_separator = [String]::Format("{0} {1} {2} ", $greetings, $first_name, $last_name)

$props = $([ordered]@{
    concat_without_separator=$concat_without_separator
    concat_with_separator=$concat_with_separator
    join_without_separator=$join_without_separator
    join_with_separator=$join_with_separator
    format_without_separator=$format_without_separator
    format_with_separator=$format_with_separator
    })

$results=@()
$results += New-Object psobject -Property $props
Write-Output $results

输出 :

concat_without_separator : HiMehvishAshiq
concat_with_separator    : Hi Mehvish Ashiq
join_without_separator   : HiMehvishAshiq
join_with_separator      : Hi Mehvish Ashiq
format_without_separator : HiMehvishAshiq
format_with_separator    : Hi Mehvish Ashiq

我们已经在[第一部分中将其与运算符(+-f-join)一起使用时详细学习了此代码](添加第一个H2的链接)。此代码示例及其逻辑与我们在上一节中使用的相同,除了一个区别之外,我们在这里使用了内置函数。

在 PowerShell 中,内置函数 concat()Join()Format() 可用于连接字符串,其中 concat() 的行为类似于 + 运算符,Join() 的行为类似于 -join() 运算符和 Format( ) 的作用类似于 -f 运算符。

使用这些方法只有两个优点。首先,我们有更有组织的代码,其次,我们对连接字符串的数量没有任何限制。

使用运算符/函数连接字符串与数字

要在 PowerShell 中连接字符串:

  • 声明并初始化两个变量;一个包含数值,另一个包含字符串类型值。
  • 使用 +-f-join 运算符连接我们在第一步中创建的所有变量的值。

使用运算符连接字符串与数字:

$count = "50"
$product = "Snacks"

$plus_without_separator = $count + $product 
$plus_with_separator = $product +" "+ $count

$f_without_separator = "{0}{1}" -f $count, $product
$f_with_separator = "{0} {1}" -f $product, $count

$join_without_separator = $count, $product -join "" 
$join_with_separator = $product, $count -join " " 

$props = $([ordered]@{
    plus_without_separator=$plus_without_separator
    plus_with_separator=$plus_with_separator
    f_without_separator=$f_without_separator
    f_with_separator=$f_with_separator
    join_without_separator=$join_without_separator
    join_with_separator=$join_with_separator
    })

$results=@()
$results += New-Object psobject -Property $props
Write-Output $results

输出 :

plus_without_separator : 50Snacks
plus_with_separator    : Snacks 50
f_without_separator    : 50Snacks
f_with_separator       : Snacks 50
join_without_separator : 50Snacks
join_with_separator    : Snacks 50

我们已经了解了+-f-join运算符,上面代码的逻辑及其使用运算符时的工作原理连接第一部分中的字符串。

在这里,我们使用这些运算符将数值与字符串类型值连接起来。请注意,如果我们在连接时将整数类型变量放在第一位或最后一位,则不会出现任何错误。我们可以使用如下内置函数将字符串与数值连接起来。

使用函数连接字符串与数字:

$count = "50"
$product = "Snacks"

$concat_without_separator = [String]::Concat($count, $product) 
$concat_with_separator = [String]::Concat($product, " ", $count)

$join_without_separator = [String]::Join("", $count, $product) 
$join_with_separator = [String]::Join(" ", $product, $count)

$format_without_separator = [String]::Format("{0}{1}", $count, $product) 
$format_with_separator = [String]::Format("{0} {1}", $product, $count)

$props = $([ordered]@{
    concat_without_separator=$concat_without_separator
    concat_with_separator=$concat_with_separator
    join_without_separator=$join_without_separator
    join_with_separator=$join_with_separator
    format_without_separator=$format_without_separator
    format_with_separator=$format_with_separator
    })

$results=@()
$results += New-Object psobject -Property $props
Write-Output $results

输出 :

concat_without_separator : 50Snacks
concat_with_separator    : Snacks 50
join_without_separator   : Snacks
join_with_separator      : Snacks 50
format_without_separator : 50Snacks
format_with_separator    : Snacks 50

使用字符串生成器

要在 PowerShell 中连接字符串:

  • 创建一个 System.Text.StringBuilder 类型的新对象。
  • 使用 append() 方法将 String 追加到其中。
  • 使用ToString()方法将其转换回字符串。

使用字符串生成器:

$first_name = "Mehvish"
$last_name = "Ashiq"
$Name = New-Object -TypeName System.Text.StringBuilder
$temp_name = $Name.Append($first_name)                 
$temp_name = $Name.Append($last_name)                 
$Name.ToString() 

输出 :

MehvishAshiq

New-Object cmdlet 用于创建 Microsoft .NET Framework 或 COM 对象的实例。我们将 -TypeName 指定为 System.Text.StringBuilder 来创建 StringBuilder 类型的对象。

我们使用 append() 方法将 $first_name$last_name 添加到 StringBuilder 对象,并使用 ToString()方法将其转换为字符串。

这就是如何在 PowerShell 中连接字符串的全部内容。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯