[玩转系统] PowerShell 多行字符串 |使用她的字符串处理多行字符串
作者:精品下载站 日期:2024-12-14 04:49:26 浏览:15 分类:玩电脑
PowerShell 多行字符串 |使用她的字符串处理多行字符串
PowerShell 多行字符串简介
字符串什么也不是,只是用“.”括起来的一组字符。多行字符串是指其值超过一行的字符串。其中,该字符串应包含在here-string(@ ” ”@) 内。换行符或回车符值也可用于创建多行字符串。还可以通过在双引号内使用换行符来定义多行字符串。本文将详细解释多行字符串及其语法使用以及适当的示例。
语法:
Here-string 用于定义多行字符串,如下所示。第一行和最后一行不应包含任何内容。
@”
First-line
Second line
Third line
“@
当我们不评估变量或表达式时,可以将字符串值括在单引号中。
示例:
Write-Host "example of multiline string using here string"
$test=@"
First-line
second line
"@
Write-Host $test
Write-Host "example using literal string"
$test1=@'
first line
$(Get-Date)
second line
'@
Write-Host $test1
输出:
即使没有here-string,多行字符串也可以定义如下
输入:
Write-Host "example of multiline without here string"
$text = {
Myself vignesh
am from chennai
am a freelancer
this is fourth line
}
write-Host $text.ToString()
输出:
使用她的字符串处理多行字符串
当 PowerShell 看到 @ ” 后跟换行符时,它会将字符串理解为多行。当它以“@”结尾时,它就结束了。其主要目的是允许用户创建带有引号、符号、换行符或其他格式文本的字符串。每当引用多行字符串引发错误时,明智的做法是首先检查第一行,并且@和“之间没有空格。与普通字符串变量一样,多行字符串也可以是原义字符串或扩展字符串。单引号表示文字,双引号表示扩展。当在单引号多字符串内使用值或表达式时,它将作为文字字符串打印,而如果在双引号多字符串内使用变量或表达式,则会对其进行求值并打印实际值,而不是比字面值。可以根据用户的需要创建四种可能的多行字符串组合。在下面的示例中,详细讨论了多行字符串的各种场景。
示例
让我们讨论 PowerShell 多行字符串的示例。
例子#1
Write-Host "Converting multine line string to a single line" -ForegroundColor Green
Write-Host "Get the contents of the file before converting" -ForegroundColor Green
Write-Host "Displaying the contents of the file" -ForegroundColor Green
$input = Get-Content -Path "C:\vignesh\multiline.txt"
foreach($line in $input)
{
Write-Host $line
}
Write-Host "Merging the contents of the file into a single line" -ForegroundColor Green
foreach ($line in $input)
{
$op = $op + $line
}
$op| Out-File C:\vignesh\multiline.txt -Force
Write-Host "After merging" -ForegroundColor Green
$final = Get-Content -Path "C:\vignesh\multiline.txt"
Write-Host $final
输出:
例子#2
Write-Host "Demo of splitting multiline string into object" -ForegroundColor Green
Write-Host " the multiline string is as follows" -ForegroundColor Green
$ip = @"
Student Name: Vignesh Student Id: 1 Age: 28
Student Name: Nandhini Student Id: 2 Age: 29
Student Name: Vyapini Student Id: 3 Age: 30
"@
Write-Host "Before splitting into object" -ForegroundColor Green
Write-Host $ip
Write-Host "After splitting the multiline string into object" -ForegroundColor Green
$ip -split "`n" |
Select-String 'Student Name: (\w+) Student Id: (\w+) Age: (.+)' |
ForEach-Object {
New-Object PSObject -Property ([Ordered] @{
"Student Name" = $_.Matches[0].Groups[1].Value
"Student Id"= $_.Matches[0].Groups[2].Value
"Age" = $_.Matches[0].Groups[3].Value
})
}
输出:
例子#3
Write-Host "Demo of different types of multi-string" -ForegroundColor Green
Write-Host "Simple example of multi-string enclosed in double quotes" -ForegroundColor Green
$ip = "
This is first line.
This is second line.
This is third line
"
Write-Host $ip
Write-Host "Simple example of multi-string in single quotes" -ForegroundColor Green
$ip = '
This is first line.
This is second line.
This is third line
'
Write-Host $ip
Write-Host "Example of here string in double quotes" -ForegroundColor Green
$ip= @"
This is first line.
This is second line.
This is third line
"@
Write-Host $ip
Write-Host "Example of here string in single quotes" -ForegroundColor Green
$ip= @'
This is first line.
This is second line.
This is third line
'@
Write-Host $ip
Write-Host "using value in double quotes" -ForegroundColor Green
$date=Get-Date
$ip=@"
Todays date is below
$date
"@
Write-Host $ip
Write-Host "using value in single quotes" -ForegroundColor Green
$date=Get-Date
$ip=@'
Todays date is below
$date
'@
Write-Host $ip
Write-Host "in single quotes the string literal is printed and not the value" -ForegroundColor Green
Write-Host "Using double quotes inside a double quotes" -ForegroundColor Green
$ip= @"
This is first line.
This is "second" line.
This is third line
"@
Write-Host $ip
Write-Host "Using single quotes inside a double quotes" -ForegroundColor Green
$ip= @"
This is first line.
This is 'second' line.
This is third line
"@
Write-Host $ip
Write-Host "Using single quotes inside a single quotes" -ForegroundColor Green
$ip= @'
This is first line.
This is 'second' line.
This is third line
'@
Write-Host $ip
Write-Host "Using double quotes inside a single quotes" -ForegroundColor Green
$ip= @'
This is first line.
This is "second" line.
This is third line
'@
Write-Host $ip
输出:
结论
因此,本文详细介绍了 PowerShell 中的多行字符串。它还详细解释了如何使用各种方法(例如here-string、单引号和双引号方法)创建多行字符串及其差异。它还演示了各种示例程序,引导我们了解如何创建多行字符串并使用它们。要了解更多详细信息,建议编写示例脚本并进行练习。
猜你还喜欢
- 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 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] 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