【问题标题】:How to add apostrophe (") to string?如何在字符串中添加撇号(“)?
【发布时间】:2017-10-04 09:25:37
【问题描述】:

我有这个 AutoIt 脚本:

ControlFocus("Open", "", "Edit1")
Sleep(500)
ControlSetText("Open", "", "Edit1", $CmdLine[1])
Sleep(500)
ControlClick("Open", "", "Button1")

它在文件选择窗口中键入文件名。我想在我的字符串之前和之后添加"(我作为命令行参数发送到我的脚本的字符串)。

我试过ControlSetText("Open", "", "Edit1", $CmdLine[1] & """),但这会导致错误:Unterminated string.

【问题讨论】:

标签: escaping autoit


【解决方案1】:

AutoIt 支持两种类型的引用,可以互换:

MsgBox(0, "single-double", 'hello " world')   ; hello " world
MsgBox(0, "double-single", "hello ' world")   ; hello ' world
MsgBox(0, "double-double", "hello "" world")  ; hello " world
MsgBox(0, "single-single", 'hello '' world')  ; hello ' world

我个人更喜欢前两个(取决于我的字符串的样子)。

所以其中一个应该这样做:

ControlSetText("Open", "", "Edit1", $CmdLine[1] & '"') ; Changing quoting where needed
ControlSetText('Open', '', 'Edit1', $CmdLine[1] & '"') ; Changing quoting consistent

【讨论】:

    【解决方案2】:

    我尝试了ControlSetText("Open", "", "Edit1", $CmdLine[1] & """),但这会导致错误:Unterminated string.

    没有尝试添加双引号之前 $CmdLine[1](仅在之后)。

    我想在我的字符串前后添加"……

    根据Documentation - Intro - Datatypes - Strings

    如果您希望字符串实际包含双引号,请使用两次……

    您也可以使用单引号……

    1. 双双引号

      根据Documentation - FAQ - 3. Why do I get errors when I try and use double quotes (") ?

      如果你想在字符串中使用双引号,那么你必须“加倍”。因此,对于您想要的每一个报价,您应该使用两个。 …

      • " 变为:

        $sString = """"

      • This is a "quoted" string. 变为:

        $sString = "This is a ""quoted"" string.".

    2. 单引号

      根据Documentation - FAQ - 3. Why do I get errors when I try and use double quotes (") ?

      ... 或使用单引号代替:...

      • " 变为:

        $sString = '"'

      • This is a "quoted" string. 变为:

        $sString = 'This is a "quoted" string.'.

    3. ASCII码

      根据Documentation - Function Reference - Chr()

      返回一个对应于 ASCII 码的字符。

      • " 变为:

        $sString = Chr(34)

      • 34 是the " -sign's ASCII code,所以This is a "quoted" string. 变为:

        $sString = "This is a " & Chr(34) & "quoted" & Chr(34) & " string."

      • 或根据StringFormat()

        $sString = StringFormat("This is a %squoted%s string.", Chr(34), Chr(34))

    Related.

    【讨论】:

      【解决方案3】:

      Autoit 使用双引号来表示字符串中的单个实例。

      例如,""""(注意,有 4 个双引号)在字符串中充当单个双引号。

      所以$CmdLine[1] & """" 应该添加一个双引号。

      请参阅 AutoIt 的文档。 https://www.autoitscript.com/autoit3/docs/intro/lang_datatypes.htm 有一个关于字符串的部分。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-11
        • 2015-02-03
        • 1970-01-01
        相关资源
        最近更新 更多