【问题标题】:Cannot replace a string by another string stored in variable不能用存储在变量中的另一个字符串替换一个字符串
【发布时间】:2018-09-11 03:29:32
【问题描述】:

我输入了这个:

$folder = 'c:\test'
"c:\test" -replace $folder ""

但得到这个错误:

在行:1 字符:25 +“c:\test”-替换$文件夹“” + ~~ 表达式或语句中出现意外的标记 '""'。 + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken

这对我没有帮助 How to handle backslash character in PowerShell -replace string operations?

【问题讨论】:

  • 你忘了,
  • 您的代码一旦修复它就不会做任何事情,因为test 不包含c:\test... 而是尝试$folder -replace 'test',''
  • @PetSerAl 谢谢我现在已经习惯了另一种语言:)
  • @JamesC。是的,事实上它并没有取代,我应该问另一个问题吗?
  • @user310291 您链接到的问题的答案已经解释了您还需要做什么。

标签: powershell


【解决方案1】:

您缺少一个逗号,我认为您的输入字符串和搜索字符串向后。

$folder -replace "test",""

【讨论】:

    猜你喜欢
    • 2013-12-03
    • 2021-06-15
    • 2014-03-13
    • 2019-05-14
    • 1970-01-01
    • 2011-04-06
    • 2017-01-28
    • 2017-03-07
    相关资源
    最近更新 更多