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

[玩转系统] 在 PowerShell 中连接字符串和变量 [8 种方法]

作者:精品下载站 日期:2024-12-14 20:34:18 浏览:13 分类:玩电脑

在 PowerShell 中连接字符串和变量 [8 种方法]


[玩转系统] 在 PowerShell 中连接字符串和变量 [8 种方法]

使用串联运算符

使用+表示的串联运算符在PowerShell中连接字符串和变量。

使用+运算符:

$variable = "together"
$concatenatedStr = "Let's learn " + $variable
Write-Host $concatenatedStr

输出 :

Let's learn together

+ 是最基本的连接运算符;我们用它来连接“Let's learn”字符串与$variable并将新字符串存储在$concatenatedStr变量中,我们进一步与 Write-Host cmdlet 一起使用以显示在 PowerShell 控制台上。

如果您不将连接的字符串存储在变量中而是将它们连接起来,请不要忘记将 "Let's learn " + $variable 表达式括在括号 (()) 内使用 Write-Host cmdlet 时;请参见以下示例。

使用+运算符:

$variable = "together"
Write-Host ("Let's learn " + $variable)

输出 :

Let's learn together

如果您不将其括在 () 中,将会发生以下情况:

使用+运算符:

$variable = "together"
Write-Host "Let's learn " + $variable

输出 :

Let's learn  + together

在 PowerShell 中使用 += 表示的串联运算符来串联字符串和变量。

使用 += 运算符:

$string1 = "together."
$string2 = "Let's learn "
$string2 += $string1
Write-Host $string2

输出 :

Let's learn together.

当我们想要一步执行连接和赋值时,可以使用+=运算符。在上面的示例中,$string1$string2 连接,并将新字符串分配给 $string2

使用 ++= 连接运算符时要小心,因为顺序非常重要。请参阅以下示例作为演示。

使用 += 运算符:

$string1 = "together."
$string2 = "Let's learn "
Write-Host ($string1 + "Are you going ")
$string1 += $string2
Write-Host $string2

输出 :

together.Are you going
Let's learn

我们将“Are you go” 字符串与$string1 变量连接起来,得到together.Are you go 作为输出。正确的串联顺序是 "Are you go " + $string1。同样,我们使用 += 运算符将 $string2$string1 连接起来,并将新字符串存储在 $string1 中>,但我们打印了包含 Let's learn 值的 $string2

使用 $() 进行字符串插值

使用 $() 进行字符串插值,在 PowerShell 中连接字符串和变量。

使用 $() 运算符:

$variable = "together"
Write-Host "Let's learn $($variable)."

输出 :

Let's learn together.

在 PowerShell 中不使用 $() 连接字符串和变量。

不使用 $():

$variable = "together"
Write-Host "Let's learn $variable."

输出 :

Let's learn together.

与使用串联运算符相比,这种方法对读者更友好且易于理解。

使用格式运算符

在 PowerShell 中使用格式运算符 (-f) 连接字符串和变量。

使用 -f 运算符:

$course1 = "Java"
$course2 = "PowerShell"
$course3 = "Python"
Write-Host ("Let's learn {0}, {1}, and {2}." -f $course1, $course2, $course3)

输出 :

Let's learn Java, PowerShell, and Python.

我们在上面的示例中使用了 -f 运算符来格式化指定的字符串。格式字符串为 “让我们学习 {0}、{1} 和 {2}。” 以及 $course1$course2$course 是我们传入的三个参数。格式说明符是 {} 内的数字,遵循 {index:format} 的格式 并对应于传入的参数索引。这些索引控制参数在 PowerShell 控制台上显示的顺序。

下面我们看另一个例子。

使用 -f 运算符:

$course1 = "Java"
$course2 = "PowerShell"
$course3 = "Python"
Write-Host ("The average of {0}, {1}, and {2} courses is {3:n2}%." -f $course1, $course2, $course3, 83.5269)

输出 :

The average of Java, PowerShell, and Python courses is 83.53%.

{3:n2}中,3是索引,而n2表示小数位数(浮动后的位数) -观点)。您可以在此处找到更多有效的格式字符串。

使用 -join 运算符

使用 -join 运算符将字符串与 PowerShell 中的变量连接起来。

使用 -join 运算符:

$course1 = "Java"
$course2 = "PowerShell"
$course3 = "Python"
Write-Host ("Available Courses:",$course1, $course2, $course3, "R" -join " ")

输出 :

Available Courses: Java PowerShell Python R

-join 运算符将所有给定的字符串与由单个空格分隔的变量连接起来。请注意,您可以使用任何您想要的分隔符。

使用join()方法

使用 join() 方法在 PowerShell 中连接字符串与变量。

使用 join() 方法:

$course1 = "Java"
$course2 = "PowerShell"
$course3 = "Python"
Write-Host ([string]::join(" ", "Available Courses:",$course1, $course2, $course3, "R"))

输出 :

Available Courses: Java PowerShell Python R

这里,我们使用了string类的join()方法;它的作用与 -join 运算符相同,它将连接由指定分隔符分隔的所有字符串和变量。在 join() 方法中,第一个参数是分隔符;剩下的就是应该连接的字符串和变量。

使用Concat()方法

使用 Concat() 方法在 PowerShell 中连接字符串与变量。

使用 Concat() 方法:

$course1 = "Java"
$course2 = "PowerShell"
$course3 = "Python"
Write-Host ([string]::Concat("Available Courses: ",$course1," ", $course2," ", $course3, " R"))

输出 :

Available Courses: Java PowerShell Python R

Concat() 方法也属于 string 类,它连接所有给定的字符串和变量,但我们必须小心分隔符。

使用 StringBuilder 类

使用 StringBuilder 类在 PowerShell 中连接字符串与变量。

使用 StringBulider 类:

$variable = "How are you?"
$stringBuilder = New-Object System.Text.StringBuilder
$stringBuilder.Append("Hello! ")
$stringBuilder.Append($variable)
Write-Host ($stringBuilder.ToString())

输出 :

Hello! How are you?
Capacity MaxCapacity Length
-------- ----------- ------
      16  2147483647      7
      32  2147483647     19

在这里,我们使用 New-Object cmdlet 实例化 StringBuilder 类,以访问其 Append() 方法,从而将字符串与变量连接起来。最后,我们使用 ToString() 方法将其转换为字符串,并使用 Write-Host cmdlet 将其打印在 PowerShell 控制台上。它还显示了有关连接字符串的其他详细信息,您可以在上面的输出中看到。

使用Join-String Cmdlet

使用 Join-String cmdlet 在 PowerShell 中连接字符串集合与变量。

使用连接字符串 Cmdlet:

$names = "How", "are", "you?"
$concatenatedStr = $names + "What's going on?" | Join-String -Separator " "
Write-Host $concatenatedStr

输出 :

How are you? What's going on?

Join-String cmdlet 用于将字符串集合组合或串联成单个字符串,该字符串由使用 -Separator 参数指定的分隔符分隔。我们将字符串集合 ($names) 通过管道传输到 Join-String cmdlet。如果您想将字符串值与字符串集合连接起来,那么您可以使用 + 运算符,就像我们在上面的示例中所做的那样。

因此,我们有多种方法将字符串与变量连接起来,但选择最适合项目要求的一种方法是最重要的。

这就是如何在 PowerShell 中连接字符串和变量。

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

取消回复欢迎 发表评论:

关灯