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

[玩转系统] PowerShell 多行字符串 |使用她的字符串处理多行字符串

作者:精品下载站 日期:2024-12-14 04:49:26 浏览:15 分类:玩电脑

PowerShell 多行字符串 |使用她的字符串处理多行字符串


[玩转系统] 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

输出:

[玩转系统] PowerShell 多行字符串 |使用她的字符串处理多行字符串

即使没有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 多行字符串 |使用她的字符串处理多行字符串

使用她的字符串处理多行字符串

当 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

输出:

[玩转系统] PowerShell 多行字符串 |使用她的字符串处理多行字符串

例子#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
})
}

输出:

[玩转系统] PowerShell 多行字符串 |使用她的字符串处理多行字符串

例子#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 多行字符串 |使用她的字符串处理多行字符串

结论

因此,本文详细介绍了 PowerShell 中的多行字符串。它还详细解释了如何使用各种方法(例如here-string、单引号和双引号方法)创建多行字符串及其差异。它还演示了各种示例程序,引导我们了解如何创建多行字符串并使用它们。要了解更多详细信息,建议编写示例脚本并进行练习。

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

取消回复欢迎 发表评论:

关灯