[玩转系统] PowerShell 多行字符串
作者:精品下载站 日期:2024-12-14 17:07:04 浏览:13 分类:玩电脑
PowerShell 多行字符串
在PowerShell中进行字符串操作时,我们经常需要打印多行字符串以便于读取、读取文件内容并将多行字符串转换为数组以进行字符串操作或将存储在变量中的多行字符串写入文件。
使用以下不同的方式创建PowerShell多行字符串
- PowerShell 回车以反引号字符 (`r`n)
- 用于创建多行字符串的换行环境变量 ([environment]::Newline)
- PowerShell Here-String 用于多行字符串 (@ ” “@)
- 定义字符串时换行
在本文中,我将向您解释在 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 中转义双引号
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[电影] 黄沙漫天(2025) 4K.EDRMAX.杜比全景声 / 4K杜比视界/杜比全景声
[风口福利] 短视频红利新风口!炬焰创作者平台重磅激励来袭
[韩剧] 宝物岛/宝藏岛/金银岛(2025)【全16集】【朴炯植/悬疑】
[电影] 愤怒的牦牛 (2025) 国语中字 4k
[短剧合集] 2025年05月30日 精选+付费短剧推荐56部
[软件合集] 25年5月30日 精选软件26个
[软件合集] 25年5月29日 精选软件18个
[短剧合集] 2025年05月28日 精选+付费短剧推荐38部
[软件合集] 25年5月28日 精选软件37个
[软件合集] 25年5月27日 精选软件26个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[办公模版] office模板合集:包含word、Excel、PowerPoint、Access四类共计2000多个模板
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag