0

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?

1
  • For all those who are wondering why I am using so old and deprecated functions. I use / enhanced activaid which is a very very old autohotkey based tool. Migration to the current version of autohotkey would a lot of work.autohotkey.com/boards/viewtopic.php?t=64118 Commented May 12, 2020 at 4:21

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Variable names in an expression are not enclosed in percent signs.

Consequently, literal strings must be enclosed in double quotes to distinguish them from variables.

To include an actual quote-character inside a literal string, specify two consecutive quotes as shown twice in this example: "She said, ""An apple a day.""".

pp_text := "Hello World"

If InStr(pp_text, " ")
    pp_text := """Hello World"""
MsgBox  % pp_text

EDIT:

To use the name of the variable (not its literal text) in the output expression, you need four quotes, as the user 0x464e explained.

pp_text := "Hello World"

If InStr(pp_text, " ")
    pp_text := """" pp_text """"
MsgBox  % pp_text

Comments

1

This is not a V2 question from the looks of it, but this is the top result I get when looking for V2 quote escaping. You use a grave accent for it, for example:

path := "`"C:\Program Files\AutoHotkey\ahk.exe`""
         ^                                    ^

Comments

0

In my AHK v2 install, this causes an error and AHK refuses to save the edited script. I've had better results after consulting the official documentation

So if my string has double quotes, as required by HTML:

"<div class="myClass">My text</div>"

I assign it to a variable (v2) this way:

MyVar := '<div class="myClass">My text</div>'

No more errors while saving. For me, anyway. Transitioning from AHK v1 to v2 in 2024 has me scratching my head from time to time, but I'm getting there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.