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

[玩转系统] PowerShell 替换字符串中的多个字符

作者:精品下载站 日期:2024-12-14 22:03:21 浏览:14 分类:玩电脑

PowerShell 替换字符串中的多个字符


PowerShell 中的 Replace() 函数将一个字符或字符串替换为另一个字符串,并返回一个字符串。由于它返回字符串,因此您可以在末尾附加 replace() 函数来替换字符串中的多个字符。

您可以使用 PowerShell Replace() 方法或 PowerShell Replace 运算符替换字符串中的多个字符。

如果您使用 PowerShell Replace() 方法,则可以多次链接 Replace() 方法来替换 PowerShell 字符串中的多个字符。

与replace()函数一样,如果您有多个要替换的字符实例,您还可以链接PowerShell替换运算符。

在本文中,我们将讨论如何使用 PowerShell Replace() 方法和 PowerShell 替换运算符来替换字符串中的多个字符。

使用replace()方法替换多个字符

PowerShell Replace() 方法返回字符串,因此您可以在字符串上附加或链接尽可能多的replace()方法来替换字符串中的多个字符。

让我们考虑一下,您在 PowerShell 中的变量 $str 中有一个字符串,如下所示。

PS C:\> $str = "Hello. to Python Programming, Learn replace string in Python?" 

Hello. to Python Programming, Learn replace string in Python?

要使用 PowerShell Replace() 方法替换多个字符,例如

.替换为空白

替换 , 为 !

代替 ?经过 !

请参阅以下 PowerShell 脚本来替换字符。

$str.Replace('.','').Replace(',','!').Replace('?','!')  

在上面的 PowerShell 脚本中,我们使用了带有两个参数的 replace() 方法;要查找的字符以及要替换为找到的字符的字符。

由于 replace() 方法返回字符串,因此我们必须将尽可能多的 replace() 方法链接在一起来替换字符串中的多个字符。

上述PowerShell脚本在替换多个字符后的输出为:

[玩转系统] PowerShell 替换字符串中的多个字符

酷提示:如何使用 PowerShell 替换字符串中的子字符串!

使用替换运算符替换字符串中的多个字符

要使用 PowerShell 替换 运算符替换字符串中的多个字符,您可以将任意多个替换运算符链接在一起。

参考上面的字符串示例,我们要使用替换运算符替换字符串中的多个字符。

PS C:\> $str = "Hello. to Python Programming, Learn replace string in Python?" 

Hello. to Python Programming, Learn replace string in Python?

在 PowerShell 中替换多个字符,如下所示

.替换为空白

替换,由 !

代替 ?经过 !

请参阅以下代码,该代码使用 PowerShell replace 运算符替换字符串中的多个字符实例。

$str -replace '\.','' -replace ',','!' -replace '\?','!' 

在上面的 PowerShell 脚本中,我们必须多次链接 replace 运算符来替换字符串中的多个字符。

PowerShell 替换运算符使用正则表达式 (regex) 进行搜索,点 (.)问号 (?) 是特殊字符。我们需要使用转义符‘\’

上述 PowerShell 脚本的输出是:

PS C:\> $str = "Hello. to Python Programming, Learn replace string in Python?"                                          
PS C:\>                                                                                                                 

PS C:\> $str -replace '\.','' -replace ',','!' -replace '\?','!'                                                        
Hello to Python Programming! Learn replace string in Python!

PS C:\>                                                                                                                           

PowerShell替换字符串中的所有字符

在 PowerShell 中,要将字符串中的所有字符替换为单个字符,请使用循环迭代字符串中的每个字符并将其替换为指定字符。

$pwd = "ShellGeek@2023"                                                                                         $newString = ""                                                                                                 $replaceChar = "x"                                                                                              for($i=0;$i -lt $pwd.Length;$i++)
{
         $newString += $replaceChar
}                                                   

$newString   

上述 PowerShell 脚本的输出将字符串 $pwd 中的所有字符替换为 x。

PS C:\> $newString                                                                                                      xxxxxxxxxxxxxx

PowerShell Get-Content 读取内容并替换文件中的多个字符串

PowerShell 中的 Get-Content cmdlet 用于读取文件的内容。要替换文件中的多个字符串,请使用替换运算符。

在以下 PowerShell 脚本中,Get-Content cmdlet 读取文件的内容并将其存储在 $fileContent 变量中。

要替换文件中的多个字符串,请使用替换运算符。替换运算符将文件中的“EU”和“AP”分别替换为“EU-01”和“AP-01”,并将修改后的内容存储在 $updatedContent 变量中。

修改后的内容通过管道运算符传递到 Set-Content cmdlet 以写入文件。

# Read the file using PowerShell Get-Content
$fileContent = Get-Content "D:\PS\comp.txt"

# use the replace operator to replace the multiple strings
$updatedContent = $fileContent -replace 'EU','EU-01' -replace 'AP','AP-01'

# use the Set-Content to save the updated content
$updatedContent | Set-Content "D:\PS\comp.txt"

在上面的PowerShell脚本中,文件中的多个字符串被替换并保存到指定位置。

上述 PowerShell 脚本的输出是:

[玩转系统] PowerShell 替换字符串中的多个字符

结论

希望上面关于如何使用 PowerShell Replace() 方法和 PowerShell Replace 运算符替换字符串中的多个字符的文章对您有所帮助。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

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

取消回复欢迎 发表评论:

关灯