How to add apostrophe (\") to string?

2020-04-20 12:42发布

问题:

I have this AutoIt script:

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

It types a file name inside a file selection window. I want to add " before and after my string (the string I send as command line argument to my script).

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

回答1:

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.



回答2:

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.



回答3:

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