autohotkey

Autohotkey: How to add double quote (") to string


I have a AutoHotkey script with:

IfInString, pp_text, %A_Space%
                {
                    pp_text := %pp_text%                                    
                }               

So in case %pp_text% contains a space I want to add " at the beginning and end

Example: pp_text = Hello World should then become pp_text = "Hello World"

How can I do this?


Solution

  • You escape a quote by by putting another quote next to it and you concatenate with the concatenate operator ., but actually you can also just omit the operator 99% of the time.

    Other things to fix in your script:
    Get rid of that super deprecated legacy command and use InStr() instead.
    And when you're in an expression, you refer to a variable by just typing its name. Those double % you're using are the legacy way of referring to a variable.
    So it's correct in the legacy command, but not in the modern := assignment.
    And also you can omit brackets on one-liner if-statements. But that's of course just going to be down personal preference.

    Full script:

    If (InStr(pp_text, A_Space))
        pp_text := """" pp_text """" 
    

    Four quotes since the the outer two specify that we're typing a string.