gpt4 book ai didi

powershell - 如何在Powershell中用美元符号替换字符串

转载 作者:行者123 更新时间:2023-12-03 11:25:28 34 4
gpt4 key购买 nike

在Powershell中给出以下字符串

$string = "this is a sample of 'my' text $PSP.what do you think"

我如何使用-replace函数将字符串转换为
this is a sample of 'my' text Hello.what do you think

我显然需要以某种方式对字符串进行转义,而且$ PSP在我的脚本中不是声明的变量

我需要更改所有其他字符串对$ PSP的提及

最佳答案

使用反引号字符(在Tab键上方):

$string = "this is a sample of 'my' text `$PSP.what do you think"

要使用-replace运算符替换美元符号,请使用反斜杠对其进行转义:
"this is a sample of 'my' text `$PSP.what do you think" -replace '\$PSP', 'hello'

或使用string.replace方法:
$string = "this is a sample of 'my' text `$PSP.what do you think"
$string.Replace('$PSP','Hello)'

这是“我的”文本的示例Hello。您如何看待

关于powershell - 如何在Powershell中用美元符号替换字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9111559/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com