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

[玩转系统] PowerShell 多行字符串

作者:精品下载站 日期:2024-12-14 17:07:04 浏览:13 分类:玩电脑

PowerShell 多行字符串


在PowerShell中进行字符串操作时,我们经常需要打印多行字符串以便于读取、读取文件内容并将多行字符串转换为数组以进行字符串操作或将存储在变量中的多行字符串写入文件。

使用以下不同的方式创建PowerShell多行字符串

  • PowerShell 回车以反引号字符 (`r`n)
  • 用于创建多行字符串的换行环境变量 ([environment]::Newline)
  • PowerShell Here-String 用于多行字符串 (@ ” “@)
  • 定义字符串时换行

[玩转系统] PowerShell 多行字符串

在本文中,我将向您解释在 PowerShell 中创建多行字符串的不同方法。

我们将讨论使用 PowerShell 将多行字符串转换为数组或在文件中存储多行的不同示例。

PowerShell 反引号字符 (`r`n)

您可以使用PowerShell特殊字符,例如回车(r)和反引号字符(`n)来创建多行字符串

让我们考虑一个使用下面的脚本定义多行字符串的示例

# Using Backtick character for multiline string

"Welcome you to `r`nShellGeek"

要创建多行字符串,我们使用 `r`n ,它将字符串分成新行。

上述脚本的输出如下

PS C:\> # Using Backtick character for multiline string

"Welcome you to `r`nShellGeek"

Welcome you to 
ShellGeek

PS C:\> 

酷提示: PowerShell 字符串连接示例!

PowerShell换行环境变量

使用 PowerShell Newline 环境变量 ([environment]::Newline) 创建多行字符串。

例如,将字符串视为“Welcome you to ShellGeek”并将该字符串分成多行字符串,使用以下脚本进行换行

# Using Newline Environent variable for multiline string
"Welcome you to {0} ShellGeek" -f [environment]::NewLine

[environment]:: Newline 环境变量将创建多行字符串,如下所示

PS C:\> # Using Newline Environent variable for multiline string
"Welcome you to {0} ShellGeek" -f [environment]::NewLine
Welcome you to 
 ShellGeek

PS C:\> 

PowerShell Here-String 定义多行字符串

您可以使用常用的PowerShell Here-String来创建多行字符串。

Here-String 以@”开头,以“@”结尾,用于声明文本块。

让我们通过一个创建多行字符串的示例来理解 Here-String,如下所示

# Using PowerShell Here-String for multiline string
@"
Welcome you to 
ShellGeek
"@

在上面的脚本中,PowerShell Here-String 在行首以“@”开头,在行尾以“@”开头。它之间包含多行字符串。

上述PowerShell Here-String的输出如下

PS C:\> # Using PowerShell Here-String for multiline string
@"
Welcome you to 
ShellGeek
"@

Welcome you to 
ShellGeek

PS C:\> 

在 PowerShell 中将字符串定义为多行字符串

您可以在定义字符串时在字符串中创建换行符,整个字符串必须用“ ”(双引号)括起来

例如,以下字符串将以多行形式创建输出

# Create Multiline string while defining string

"Welcome you to
ShellGeek"

上述脚本的输出如下

PS C:\> # Create Multiline string while defining string

"Welcome you to
ShellGeek"
Welcome you to
ShellGeek

PS C:\> 

酷提示:如何在 PowerShell 中创建多行注释!

带双引号的 PowerShell 多行字符串

字符串中通常包含双引号,如果您想创建带双引号的多行字符串,可以使用 PowerShell Here-String 轻松完成。

#Using PowerShell Here-String for multiline string with double quotes
@"
Welcome you to
"ShellGeek"
"@

在上面的PowerShell脚本中,ShellGeek的字符串带有双引号,我们使用PowerShell Here-String来创建多行命令。

上述脚本的输出如下

PS C:\> #Using PowerShell Here-String for multiline string with double quotes
@"
Welcome you to
"ShellGeek"
"@

Welcome you to
"ShellGeek"

PS C:\> 

酷提示:如何在 PowerShell 中将字符串转换为日期时间!

带单引号的 PowerShell 多行字符串

如果要定义带单引号的多行字符串,请使用 PowerShell Here-String 定义带引号的字符串,如下所示

#Using PowerShell Here-String for multiline string with single quotes
@"
Welcome you to
'ShellGeek'
"@

在上面的PowerShell脚本中,一个字符串有一个ShellGeek的单引号,我们使用PowerShell Here-String来创建多行命令。

上述脚本的输出如下

PS C:\> #Using PowerShell Here-String for multiline string with single quotes
@"
Welcome you to
'ShellGeek'
"@

Welcome you to
'ShellGeek'

PS C:\> 

酷提示:如何在 PowerShell 中计算文件中的行数!

PowerShell 多行字符串到参数

在进行字符串操作时,我们必须将多行字符串存储到参数。您可以使用 PowerShell Here-String 将多行字符串存储到参数中,如下所示

#Using PowerShell Here-String for multiline string to Parameter

$test = @"
Welcome you to
ShellGeek 
"@

在上面的 PowerShell 脚本中,我们创建了一个 $test 变量来保存使用 PowerShell Here-String 创建的多行字符串。

当我们打印 $test 时,它会打印多行字符串,如下所示

PS C:\> #Using PowerShell Here-String for multiline string to Parameter

$test = @"
Welcome you to
ShellGeek 
"@

PS C:\> $test
Welcome you to
ShellGeek 

PS C:\> 

酷提示:如何使用 PowerShell 制表符在字符串中插入制表符!

PowerShell 多行字符串到数组

大多数在读取文件后进行字符串操作时使用多行字符串,所有数据都存储在变量中的多行字符串中。

如果您需要执行任何类型的字符串操作,请将多行字符串转换为数组,然后对数组元素执行该任务。

例如,在下面的脚本中,我们有一个使用 PowerShell Here-String 具有多行字符串的变量。

#Using PowerShell Here-String for multiline string to array

$test = @'
Welcome you to
ShellGeek
'@

$arrTest = $test.Split("`r`n")
Write-Host "Total Elements in array-->" $arrTest.Count

Write-Host "First element in array-->" $arrTest[0]

Write-Host "Second element in array-->" $arrTest[2]

在上面的 PowerShell 脚本中,我们通过 `r`n 使用 split 函数将其分割为多行字符串。它返回一个多行数组,并存储在 $arrTest 数组变量中。

使用 Write-Host,我们打印多行字符串的数组计数、数组中的第一个元素等。

酷提示:如何在 PowerShell 中创建多行命令!

结论

在上面的文章中,我们学习了如何使用不同的方式在 PowerShell 中创建多行字符串并将多行字符串存储在参数或数组中。

PowerShell Here-String 是定义多行字符串最常用的技术。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

推荐内容

在 PowerShell 中转义双引号

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

取消回复欢迎 发表评论:

关灯