[玩转系统] PowerShell 连接字符串:示例和函数 | PowerShell教育大学商学院
作者:精品下载站 日期:2024-12-14 04:54:11 浏览:15 分类:玩电脑
PowerShell 连接字符串:示例和函数 | PowerShell教育大学商学院
PowerShell 连接字符串简介
PowerShell 字符串连接是组合一个或多个字符串的过程。字符串连接主要是使用“+”运算符来实现的。还有其他方法,例如将字符串括在双引号内、使用连接运算符或使用 -f 运算符。
目录
- 介绍
- 特征
- 示例
- 其他常用的字符串函数
- 结论
- 常见问题解答
语法:
‘+’运算符用于连接字符串。让我们看下面的示例:
代码:
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 的主要功能和说明:
- 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"
输出:
示例 #2 - 字符串和整数连接
代码:
Write-Host "String concatenation demo of int and string"
$string1=75
$string2="my name is Alex"
$string3=$string1 + $string2
输出:
在上面的代码中,如果在连接之前交换变量,则不会出现错误。
输入:
Write-Host "String concatenation demo of int and string"
$string1=75
$string2="my name is Alex"
$string3=$string2 + $string1
Write-Host $string3
输出:
或者,可以通过使用字符串替换来实现上述目的。
代码:
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)"
输出:
示例 #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
输出:
示例 #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 "!!!"
输出:
示例 #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 ''
输出:
示例 #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()
输出:
例子#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"
输出:
例子#8
连接 CSV 中的两个列值并将它们导出到新列。
在下面的示例中,CSV 有两列 - FirstName 和 LastName。下面的脚本将合并两个列值并将它们导出到名为 FullName 的新列。
运行脚本前的文件内容:
FirstName姓氏
Vignesh克里希纳库玛尔
Nandhini维涅什
Sampath查鲁
Vyapini维涅什
Krishnakumar桑卡兰
Durga克里希纳库玛尔
代码:
#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"
输出:
例子#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
输出:
其他常用的字符串函数
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("-")
输出:
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 连接字符串
本文详细介绍了字符串连接。它演示了除“+”运算符之外的各种字符串组合方式。还演示了如何实现字符串连接的各种示例。它还涵盖了各种字符串方法,如字符串连接、字符串比较、字符串替换等。
常见问题 (FAQ)
Q1。在 PowerShell 中连接字符串的可能方法是什么?
答案:首先使用管理员权限访问“PowerShell ISE”。要执行相同的操作,请转到搜索栏查找“PowerShell ISE”。获得搜索结果后,右键单击“PowerShell ISE”应用程序,然后选择“以管理员身份运行”。
Q2。如何在 PowerShell 中替换字符串?
答案:在PowerShell中,使用简单的replace()运算符可以轻松地将现有字符串替换为新字符串。编译器找不到任何首选字符串;它不响应任何内容。
Q3。如何在PowerShell中表示变量?
答案:变量是数据存储单位。在 PowerShell 中,变量仅表示以美元符号开头的文本字符串。例如$text、$value等
猜你还喜欢
- 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