【问题标题】:How to define string that contains the symbols double and single quote at the same time, in Powerhsell?如何在 Powerhsell 中定义同时包含符号双引号和单引号的字符串?
【发布时间】:2021-12-08 06:38:49
【问题描述】:

我需要将以下文本格式放入变量中

"Sometext":"more text";'still text

但是,因为文本有双引号和单引号,我不能把它放在一个字符串中。我试过使用@''@ 和@""@,但它不起作用。 旁注:我无法编辑文本,因为它是自动生成的 我能做什么?

提前谢谢你

【问题讨论】:

  • "我试过使用@''@ 和@""@ 但它不起作用" 请向我们展示您之前的尝试并描述发生了什么(请随时发布任何出现的错误消息)

标签: string powershell


【解决方案1】:

如果是字面量,可以使用here string:

$variable = @"
"Sometext":"more text";'still text
"@

(请注意,最后的 "@ 必须位于单独的一行,位于该行的最开头。)

【讨论】:

  • 谢谢,成功了!
【解决方案2】:

要构建带有复杂引号的字符串,请考虑composite formatting。将引号保存在变量中并使用格式化占位符插入它们。像这样,

$squote = "'"
$dquote = '"
$myString = "{0}Sometext{0}:{0}more text{0};{1}still text" -f $dquote, $squote
$myString
"Sometext":"more text";'still text

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多