How to add apostrophe (") to string?

前端 未结 3 740
终归单人心
终归单人心 2021-01-23 14:19

I have this AutoIt script:

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


        
相关标签:
3条回答
  • 2021-01-23 14:45

    I tried ControlSetText("Open", "", "Edit1", $CmdLine[1] & """) but this results into error: Unterminated string..

    No attempt to add double quote before $CmdLine[1] was made (only after).

    I want to add " before and after my string …

    As per Documentation - Intro - Datatypes - Strings:

    If you want a string to actually contain a double-quote use it twice …

    You can also use single-quotes …

    1. Double double-quote

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

      If you want to use double-quotes inside a string then you must "double them up". So for every one quote you want you should use two. …

      • " becomes:

        $sString = """"

      • This is a "quoted" string. becomes:

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

    2. Single quote

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

      … or use single quotes instead: …

      • " becomes:

        $sString = '"'

      • This is a "quoted" string. becomes:

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

    3. ASCII code

      As per Documentation - Function Reference - Chr() :

      Returns a character corresponding to an ASCII code.

      • " becomes:

        $sString = Chr(34)

      • 34 is the " -sign's ASCII code, so This is a "quoted" string. becomes:

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

      • Or per StringFormat() :

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

    Related.

    0 讨论(0)
  • 2021-01-23 14:49

    AutoIt supports two types of quoting, which are interchangeable:

    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
    

    I personally prefer the first two (depending on how my string looks like).

    So one of those should do:

    ControlSetText("Open", "", "Edit1", $CmdLine[1] & '"') ; Changing quoting where needed
    ControlSetText('Open', '', 'Edit1', $CmdLine[1] & '"') ; Changing quoting consistent
    
    0 讨论(0)
  • 2021-01-23 14:51

    Autoit uses pairs quotes to indicate a single instance within a string.

    E.g., """" (notice, there are 4 double quotation marks) acts as a single double-quote within the string.

    So $CmdLine[1] & """" should add a single double-quote.

    See AutoIt's documentation. https://www.autoitscript.com/autoit3/docs/intro/lang_datatypes.htm has a section on strings.

    0 讨论(0)
提交回复
热议问题