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

[玩转系统] PowerShell 连接字符串:示例和函数 | PowerShell教育大学商学院

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

PowerShell 连接字符串:示例和函数 | PowerShell教育大学商学院


PowerShell 连接字符串简介

PowerShell 字符串连接是组合一个或多个字符串的过程。字符串连接主要是使用“+”运算符来实现的。还有其他方法,例如将字符串括在双引号内、使用连接运算符或使用 -f 运算符。

[玩转系统] PowerShell 连接字符串:示例和函数 | PowerShell教育大学商学院

目录
  • 介绍
  • 特征
  • 示例
  • 其他常用的字符串函数
  • 结论
  • 常见问题解答

语法:

‘+’运算符用于连接字符串。让我们看下面的示例:

代码:

Write-Host "String Concat in Powershell"
Write-Host "Basic Example"
$str1="My name is John."
$str2="Am from Africa"
Write-Host "Concatenated text is below"
$str1+$str2

输出:

[玩转系统] PowerShell 连接字符串:示例和函数 | PowerShell教育大学商学院

主要亮点

  • PowerShell 是可自定义的独立软件,供应商和企业开发人员可以自定义他们的工具和实用程序。
  • 连接是一种使用某些运算符连接两个字符串的方法。
  • PowerShell 字符串连接通过字符串生成器、带有数字的字符串、内置函数和运算符进行。

PowerShell 的功能

PowerShell 的主要功能和说明:

  • Get-Command 创建来自 cmdlet 和设备功能的所有命令的列表。 Cmdlet 管理注册表、进程、服务、事件日志和其他系统管理功能。它使用 Windows Management Instrumentation (WMI) 来执行所有这些功能。
  • get-Help cmdlet 可帮助用户详细了解 PowerShell 及其特定组件的原理。
  • 使用 Windows Management Instrumentation 和 WS-Management,管理员可以远程操作设备。
  • 使用 PowerShell 管道运算符 (|),您可以将命令链接在一起。在这种方法中,一个命令的输出是以管道方式作为即将到来的命令的输入。 PowerShell 管道运算符允许对象(而不是文本字符串)从一个 cmdlet 流到另一个 cmdlet。
  • PowerShell脚本语言为现有脚本和命令行工具提供支持。
  • cmdlet 和系统数据存储具有通用的命名约定,并且它们使用通用语法使数据共享变得容易。
  • 基于命令的导航以简化的方式允许用户导航注册表和其他数据存储。
  • PowerShell 拥有强大的对象操作功能。对象可以直接发送到其他工具或数据库。

PowerShell 连接字符串的示例

不必仅使用“+”号来连接字符串。此示例将展示连接字符串的各种方式。

示例 #1 - 使用分隔符

代码:

Write-Host "String concatenation without + operator"
$str1="first sentence"
$str2="second sentence"
Write-Host "After concatenation"
"$str1$str2"
Write-Host "Example of concatenation with separators"
$str1+ "," +$str2
$str3="Powershell is the most underrated language"
$str4=" it can do many wonders"
Write-Host "Another way"
"$str3!!!!,$str4"

输出:

[玩转系统] PowerShell 连接字符串:示例和函数 | PowerShell教育大学商学院

示例 #2 - 字符串和整数连接

代码:

Write-Host "String concatenation demo of int and string"
$string1=75
$string2="my name is Alex"
$string3=$string1 + $string2

输出:

[玩转系统] PowerShell 连接字符串:示例和函数 | PowerShell教育大学商学院

在上面的代码中,如果在连接之前交换变量,则不会出现错误。

输入:

Write-Host "String concatenation demo of int and string"
$string1=75
$string2="my name is Alex"
$string3=$string2 + $string1
Write-Host $string3

输出:

[玩转系统] PowerShell 连接字符串:示例和函数 | PowerShell教育大学商学院

或者,可以通过使用字符串替换来实现上述目的。

代码:

Write-Host "String concatenation demo of int and string"
$int1=75
$string2="my name is Alex"
Write-Host "string concatenation using string Substitution"
"$($int1) , $($string2)"

输出:

[玩转系统] PowerShell 连接字符串:示例和函数 | PowerShell教育大学商学院

示例 #3 - 使用 -f 运算符

代码:

Write-Host "String concatenation using f operator"
$str1="This is the first sentence"
$str2="This is the second sentence"
$str3="This is the third Sentence"
Write-Host "Concatenated string is below"
"{0}.{1}.{2}." -f $str1,$str2,$str3

输出:

[玩转系统] PowerShell 连接字符串:示例和函数 | PowerShell教育大学商学院

示例 #4 - 使用连接运算符

代码:

Write-Host "String concatenation using Join operator"
$str1="This is the first sentence"
$str2="This is the second sentence"
$str3="This is the third Sentence"
Write-Host "Concatenated string is below"
$str1,$str2,$str3 -join "!!!"

输出:

[玩转系统] PowerShell 连接字符串:示例和函数 | PowerShell教育大学商学院

示例 #5 - 删除写入主机中的默认空间

代码:

$str1="This is the first sentence"
$str2="This is the second sentence"
$str3="This is the third Sentence"
Write-Host "Concatenated string is below" $str1 $str2 $str3 -Separator ''

输出:

[玩转系统] PowerShell 连接字符串:示例和函数 | PowerShell教育大学商学院

示例 #6 - 使用 Concat() 和字符串生成器

代码:

Write-Host "String concatenation using concat method"
$str1="This is the first sentence"
$str2="This is the second sentence"
$str3="This is the third Sentence"
Write-Host "Concatenated string is below"
[System.String]::Concat($str1,".",$str2,".",$str3)
Write-Host "String concatenation using string builder"
$SB = New-Object -TypeName System.Text.StringBuilder
$null = $SB.Append("First Sentence")
$null = $SB.Append("Second Sentence")
$null = $SB.Append("Third Sentence")
$null = $SB.Append("Fourth Sentence")
$SB.ToString()

输出:

[玩转系统] PowerShell 连接字符串:示例和函数 | PowerShell教育大学商学院

例子#7

代码:

$string1 = @"
My name is Ella
Am from USA
Am a freelancer
"@
$string2 = @"
First sentence of the example
Second sentence of the example
Third Sentence of the example
"@
$string1 , $string2 -join "`n"

输出:

[玩转系统] PowerShell 连接字符串:示例和函数 | PowerShell教育大学商学院

例子#8

连接 CSV 中的两个列值并将它们导出到新列。

在下面的示例中,CSV 有两列 - FirstName 和 LastName。下面的脚本将合并两个列值并将它们导出到名为 FullName 的新列。

运行脚本前的文件内容:

FirstName

姓氏

Vignesh

克里希纳库玛尔

Nandhini

维涅什

Sampath

查鲁

Vyapini

维涅什

Krishnakumar

桑卡兰

Durga

克里希纳库玛尔

[玩转系统] PowerShell 连接字符串:示例和函数 | PowerShell教育大学商学院

代码:

#Reading the csv by importing
$CSV = import-csv "C:\Users\R003646\Desktop\Sample.csv" | select FirstName,LastName,
@{n='FullName';e={$_.FirstName + " " + $_.LastName}}
# script @{n='FullName";e={$_.FirstName + " " + $_.LastName}} will create the new column FullName and concatenate the FirstName and LastName Column Value
# Export the data from the Variable $CSV to the new file
$CSV | export-CSV "C:\Users\R003646\Desktop\Sample.csv"

输出:

[玩转系统] PowerShell 连接字符串:示例和函数 | PowerShell教育大学商学院

[玩转系统] PowerShell 连接字符串:示例和函数 | PowerShell教育大学商学院

例子#9

在这里,我们将看到连接两个或多个路径如何生成单个路径。 Join-Path cmdlet 将两个或多个路径合并为一个路径。

语法:

NAME
Join-Path
SYNTAX
Join-Path [-Path] <string[]> [-ChildPath] <string> [-Resolve] [-Credential <pscredential>] [-UseTransaction] [<CommonParameters>]
ALIASES
None

E示例#10

输入:

Write-Host "Welcome to Join Path example"
$path1= "C:\test"
$path2="D:\test"
$path3= "E:\test"
$path4="F:\test"
$path5="G:\test"
Write-Host "Appending new path"
Join-Path -Path $path1, $path2, $path3, $path4, $path5 -ChildPath test1

输出:

[玩转系统] PowerShell 连接字符串:示例和函数 | PowerShell教育大学商学院

其他常用的字符串函数

1. 分割()

这是另一种可用于将字符串拆分为子字符串的方法。

语法:

.Split(strSeparator [, MaxSubstrings] [, Options])
String -Split strSeparator [, MaxSubstrings] [, Options]
String -Split {scriptblock} [, MaxSubstrings]
-Split String
  • strSeparator:分割字符串的标识字符
  • MaxSubstrings:可以生成的最大子字符串数

示例:

Write-Host "Generating substring using split method"
$teststring="My name is Joseph- am from UK"
Write-Host "splitting using - character"
$teststring -split "-"
$teststring="domainname\username"
Write-Host "Splitting using \ character"
$teststring -split "\"
Write-Host "generating substring using space"
$string="string1 string2 strin3"
$string.Split("")
Write-Host "splitting using multiple separators"
$string="domain\systems-test"
$string.Split("\").Split("-")

输出:

[玩转系统] PowerShell 连接字符串:示例和函数 | PowerShell教育大学商学院

2. 替换功能

当涉及到字符串时,替换字符串或子字符串的一部分是一个积分操作。 PowerShell 用户总是需要查找文本并将其替换为其他文本片段。这是通过 Replace() 方法实现的。

语法:

Replace(strOldChar, strNewChar)
Stroldchar: Character to be found
Strnewchar: character to be replace the found text

示例:

代码:

Write-Host "Replacing a text in string"
$test="Old text to be replaced"
Write-Host "Going to replace old"
$test.Replace("old","New")
Write-Host "Text is replaced"

输出:

[玩转系统] PowerShell 连接字符串:示例和函数 | PowerShell教育大学商学院

结论 - PowerShell 连接字符串

本文详细介绍了字符串连接。它演示了除“+”运算符之外的各种字符串组合方式。还演示了如何实现字符串连接的各种示例。它还涵盖了各种字符串方法,如字符串连接、字符串比较、字符串替换等。

常见问题 (FAQ)

Q1。在 PowerShell 中连接字符串的可能方法是什么?

答案:首先使用管理员权限访问“PowerShell ISE”。要执行相同的操作,请转到搜索栏查找“PowerShell ISE”。获得搜索结果后,右键单击“PowerShell ISE”应用程序,然后选择“以管理员身份运行”。

Q2。如何在 PowerShell 中替换字符串?

答案:在PowerShell中,使用简单的replace()运算符可以轻松地将现有字符串替换为新字符串。编译器找不到任何首选字符串;它不响应任何内容。

Q3。如何在PowerShell中表示变量?

答案:变量是数据存储单位。在 PowerShell 中,变量仅表示以美元符号开头的文本字符串。例如$text、$value等

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

取消回复欢迎 发表评论:

关灯