[玩转系统] 在 PowerShell 中连接字符串 [4 种方法]
作者:精品下载站 日期:2024-12-14 05:24:19 浏览:13 分类:玩电脑
在 PowerShell 中连接字符串 [4 种方法]
使用运算符
要在 PowerShell 中连接字符串:
- 声明并初始化包含字符串类型值的变量。
- 使用
+
、-f
和-join
运算符连接我们在第一步中创建的所有变量的值。
使用运算符:
$greetings = "Hi"
$first_name = "Mehvish"
$last_name = "Ashiq"
$plus_without_separator = $greetings + $first_name + $last_name
$plus_with_separator = $greetings +" "+ $first_name +" "+ $last_name
$f_without_separator = "{0}{1}{2}" -f $greetings, $first_name, $last_name
$f_with_separator = "{0} {1} {2}" -f $greetings, $first_name, $last_name
$join_without_separator = $greetings, $first_name, $last_name -join ""
$join_with_separator = $greetings, $first_name, $last_name -join " "
$props = $([ordered]@{
plus_without_separator=$plus_without_separator
plus_with_separator=$plus_with_separator
f_without_separator=$f_without_separator
f_with_separator=$f_with_separator
join_without_separator=$join_without_separator
join_with_separator=$join_with_separator
})
$results=@()
$results += New-Object psobject -Property $props
Write-Output $results
输出 :
plus_without_separator : HiMehvishAshiq
plus_with_separator : Hi Mehvish Ashiq
f_without_separator : HiMehvishAshiq
f_with_separator : Hi Mehvish Ashiq
join_without_separator : HiMehvishAshiq
join_with_separator : Hi Mehvish Ashiq
我们可以使用 +
、-f
和 -join
运算符在 PowerShell 中连接带/不带分隔符的字符串。下面我们来了解一下这些运算符的用途和区别。
首先,我们声明并初始化了三个变量,$greetings
、$first_name
和 $last_name
,其中包含 Hi
、
分别。但是,它与 +
运算符不同,因为它允许我们通过更改变量的位置来将字符串放在特定索引上。
我们可以在每个大括号对之间放置一个空格,以将字符串彼此分开。大括号中的数字是索引。我们可以更改值的顺序而无需重新排列变量,如下所示。
使用 -f 运算符:
$greetings = "Hi"
$first_name = "Mehvish"
$last_name = "Ashiq"
$result = "{2}{1}{0}" -f $greetings, $first_name, $last_name
$result
输出 :
AshiqMehvishHi
我们使用的第三个运算符是 -join
运算符;它也像 +
和 -f
运算符一样工作; -join
运算符的好处是我们不必重复编写分隔符,而是在 -join
参数之后传递一次。
我们将结果保存在 $plus_without_separator
、$plus_with_separator
、$f_without_separator
、$f_with_separator
、$join_without_separator 中使用
、+
、-f
和 -join
运算符后得到的 $join_with_separator
变量有和没有分隔符。
我们可以在 PowerShell 控制台上单独编写这些变量来显示输出,但我们采用了不同的方法来同时查看所有结果。为此,我们使用 @{}
创建一个哈希表,这是一种具有特定值的唯一键的数据结构;我们用它来拥有键值格式的属性。
接下来,我们将 @{}
与 $ ([ordered])
括起来,作为 $ ([ordered]@{})
按照属性的创建顺序拥有属性;否则,它们将随机显示。请参阅以下输出以获得清晰的图片。
输出 :
join_without_separator : HiMehvishAshiq
join_with_separator : Hi Mehvish Ashiq
f_without_separator : HiMehvishAshiq
plus_with_separator : Hi Mehvish Ashiq
f_with_separator : Hi Mehvish Ashiq
plus_without_separator : HiMehvishAshiq
此时,我们使用哈希表创建了一个包含我们定义的所有属性的对象,并将其保存在 $props
变量中。如何?让我们看一下。 New-Object
创建了一个对象,而 -Property
参数设置了每个属性值,并按照它们在哈希表中出现的顺序调用了新创建对象的每个方法。
请记住,哈希表包含键值格式的数据,其中键是方法或属性的名称,而值是方法参数或属性值。那么现在的重点是,我们为什么要使用 PSObject 类?
我们使用 PSObject
类,因为如果从 PSObject
类派生一个新对象,并且我们编写了该对象上不存在的属性,则 New -Object
将把我们指定的属性作为 NoteProperty
添加到我们的对象中。因此,对于这种特殊情况,如果我们不使用 PSObject
类,New-Object
命令将产生一个非终止错误。
我们将对象的数据保存在使用数组运算符 (@()
) 创建的 $result
数组中,最后,我们使用了 Write-Output
cmdlet 在 PowerShell 控制台上打印指定变量的值。
请注意,Write-Output
会将对象发送到名为 Output Stream
或 Success Pipeline
的主管道。此 cmdlet 用于在 PowerShell 控制台上显示对象和字符串。例如,如果出现错误,我们将使用 Write-Error 将错误对象发送到错误管道。
使用内置函数
要在 PowerShell 中连接字符串:
- 声明并初始化三个包含字符串类型值的变量。
- 使用
concat()
、Join()
和Format()
函数连接我们在第一步中创建的所有变量的值。
使用内置函数:
$greetings = "Hi"
$first_name = "Mehvish"
$last_name = "Ashiq"
$concat_without_separator = [String]::Concat($greetings, $first_name, $last_name)
$concat_with_separator = [String]::Concat($greetings, " ", $first_name, " ", $last_name)
$join_without_separator = [String]::Join("", $greetings, $first_name, $last_name)
$join_with_separator = [String]::Join(" ", $greetings, $first_name, $last_name)
$format_without_separator = [String]::Format("{0}{1}{2}", $greetings, $first_name, $last_name)
$format_with_separator = [String]::Format("{0} {1} {2} ", $greetings, $first_name, $last_name)
$props = $([ordered]@{
concat_without_separator=$concat_without_separator
concat_with_separator=$concat_with_separator
join_without_separator=$join_without_separator
join_with_separator=$join_with_separator
format_without_separator=$format_without_separator
format_with_separator=$format_with_separator
})
$results=@()
$results += New-Object psobject -Property $props
Write-Output $results
输出 :
concat_without_separator : HiMehvishAshiq
concat_with_separator : Hi Mehvish Ashiq
join_without_separator : HiMehvishAshiq
join_with_separator : Hi Mehvish Ashiq
format_without_separator : HiMehvishAshiq
format_with_separator : Hi Mehvish Ashiq
我们已经在[第一部分中将其与运算符(+
、-f
和 -join
)一起使用时详细学习了此代码](添加第一个H2的链接)。此代码示例及其逻辑与我们在上一节中使用的相同,除了一个区别之外,我们在这里使用了内置函数。
在 PowerShell 中,内置函数 concat()
、Join()
和 Format()
可用于连接字符串,其中 concat()
的行为类似于 +
运算符,Join()
的行为类似于 -join()
运算符和 Format( )
的作用类似于 -f
运算符。
使用这些方法只有两个优点。首先,我们有更有组织的代码,其次,我们对连接字符串的数量没有任何限制。
使用运算符/函数连接字符串与数字
要在 PowerShell 中连接字符串:
- 声明并初始化两个变量;一个包含数值,另一个包含字符串类型值。
- 使用
+
、-f
和-join
运算符连接我们在第一步中创建的所有变量的值。
使用运算符连接字符串与数字:
$count = "50"
$product = "Snacks"
$plus_without_separator = $count + $product
$plus_with_separator = $product +" "+ $count
$f_without_separator = "{0}{1}" -f $count, $product
$f_with_separator = "{0} {1}" -f $product, $count
$join_without_separator = $count, $product -join ""
$join_with_separator = $product, $count -join " "
$props = $([ordered]@{
plus_without_separator=$plus_without_separator
plus_with_separator=$plus_with_separator
f_without_separator=$f_without_separator
f_with_separator=$f_with_separator
join_without_separator=$join_without_separator
join_with_separator=$join_with_separator
})
$results=@()
$results += New-Object psobject -Property $props
Write-Output $results
输出 :
plus_without_separator : 50Snacks
plus_with_separator : Snacks 50
f_without_separator : 50Snacks
f_with_separator : Snacks 50
join_without_separator : 50Snacks
join_with_separator : Snacks 50
我们已经了解了+
、-f
和-join
运算符,上面代码的逻辑及其使用运算符时的工作原理连接第一部分中的字符串。
在这里,我们使用这些运算符将数值与字符串类型值连接起来。请注意,如果我们在连接时将整数类型变量放在第一位或最后一位,则不会出现任何错误。我们可以使用如下内置函数将字符串与数值连接起来。
使用函数连接字符串与数字:
$count = "50"
$product = "Snacks"
$concat_without_separator = [String]::Concat($count, $product)
$concat_with_separator = [String]::Concat($product, " ", $count)
$join_without_separator = [String]::Join("", $count, $product)
$join_with_separator = [String]::Join(" ", $product, $count)
$format_without_separator = [String]::Format("{0}{1}", $count, $product)
$format_with_separator = [String]::Format("{0} {1}", $product, $count)
$props = $([ordered]@{
concat_without_separator=$concat_without_separator
concat_with_separator=$concat_with_separator
join_without_separator=$join_without_separator
join_with_separator=$join_with_separator
format_without_separator=$format_without_separator
format_with_separator=$format_with_separator
})
$results=@()
$results += New-Object psobject -Property $props
Write-Output $results
输出 :
concat_without_separator : 50Snacks
concat_with_separator : Snacks 50
join_without_separator : Snacks
join_with_separator : Snacks 50
format_without_separator : 50Snacks
format_with_separator : Snacks 50
使用字符串生成器
要在 PowerShell 中连接字符串:
- 创建一个 System.Text.StringBuilder 类型的新对象。
- 使用
append()
方法将 String 追加到其中。 - 使用
ToString()
方法将其转换回字符串。
使用字符串生成器:
$first_name = "Mehvish"
$last_name = "Ashiq"
$Name = New-Object -TypeName System.Text.StringBuilder
$temp_name = $Name.Append($first_name)
$temp_name = $Name.Append($last_name)
$Name.ToString()
输出 :
MehvishAshiq
New-Object
cmdlet 用于创建 Microsoft .NET Framework 或 COM 对象的实例。我们将 -TypeName
指定为 System.Text.StringBuilder
来创建 StringBuilder
类型的对象。
我们使用 append()
方法将 $first_name
和 $last_name
添加到 StringBuilder 对象,并使用 ToString()
方法将其转换为字符串。
这就是如何在 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 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 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