Trouble with StringReplace * ? ;

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Trouble with StringReplace * ? ;

Post by PuzzledGreatly » 12 Oct 2013, 21:21

This works for ;

Code: Select all

Stringreplace, SearchTerm, SearchTerm, `;, %A_space%
But none of these work for * and ? (nothing is replaced)

Code: Select all

Stringreplace, SearchTerm, SearchTerm, `*, %A_space%
Stringreplace, SearchTerm, SearchTerm, `?, %A_space%
Stringreplace, SearchTerm, SearchTerm, *, %A_space%
Stringreplace, SearchTerm, SearchTerm, ?, %A_space%
How do I turn * and ? into a space? Could RegExReplace() replace all three in one go? I haven't been able to get my head around RegEx.

kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Trouble with StringReplace * ? ;

Post by kon » 12 Oct 2013, 21:53

Code: Select all

text = 
(
line1;blah
line2*blah
line3?blah
)

Stringreplace, text, text, `;, %A_space%, All
MsgBox, % text

Stringreplace, text, text, *, %A_space%, All
MsgBox, % text

Stringreplace, text, text, ?, %A_space%, All
MsgBox, % text

text = 
(
line1;blah
line2*blah
line3?blah
)

MsgBox, % RegexReplace(text, ";|\*|\?", " ")

User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Trouble with StringReplace * ? ;

Post by PuzzledGreatly » 12 Oct 2013, 23:20

Thanks, the RegexReplace worked but the Stringreplace didn't. Could it be because in my actual code I am using it inside a function?

tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Trouble with StringReplace * ? ;

Post by tmplinshi » 13 Oct 2013, 00:32

Code: Select all

MsgBox, % Test("*123*?")

Test(var)
{
	Stringreplace, var, var, *, %A_space%, All
	Stringreplace, var, var, ?, %A_space%, All
	Return var
}

User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Trouble with StringReplace * ? ;

Post by PuzzledGreatly » 13 Oct 2013, 02:36

Thanks, I have something similar but there is an issue with * and ? - these are being ignored by stringreplace on my PC.

Tyrer
Posts: 15
Joined: 05 Oct 2013, 02:03

Re: Trouble with StringReplace * ? ;

Post by Tyrer » 13 Oct 2013, 06:16

tmplinshi's code works perfectly. Could it be a problem with global variables in functions? ie. the following won't work:

Code: Select all

 var=*123*?

Test()
{
	
    Stringreplace, var, var, *, %A_space%, All
    Stringreplace, var, var, ?, %A_space%, All
}
Test()
msgbox %var%

but this will:

Code: Select all

 var=*123*?

Test()
{
	global Var
    Stringreplace, var, var, *, %A_space%, All
    Stringreplace, var, var, ?, %A_space%, All
}
Test()
msgbox %var%

User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Trouble with StringReplace * ? ;

Post by PuzzledGreatly » 13 Oct 2013, 07:40

Thanks try this one - the ? is removed but the * isn't:

Code: Select all

         var=*123*?
         
        Test(Var)
        {
            static Var1
            Stringreplace, var1, var, *, %A_space%, All
            Stringreplace, var1, var, ?, %A_space%, All
			msgbox %var1%
        }
        Test(var)

Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: Trouble with StringReplace * ? ;

Post by Guest10 » 13 Oct 2013, 08:04

could this work static Var >> global Var?

Tyrer
Posts: 15
Joined: 05 Oct 2013, 02:03

Re: Trouble with StringReplace * ? ;

Post by Tyrer » 13 Oct 2013, 08:31

The code doesn't work because in the first stringreplace, var1 becomes var with * replaced. In the second stringreplace, var1 now becomes the original var with the ? replaced.
This would work:

Code: Select all

var=*123*?
         
        Test(Var)
        {
            static Var1
            Stringreplace, var1, var, *, %A_space%, All
            Stringreplace, var1, var1, ?, %A_space%, All
         msgbox %var1%
        }
        Test(var)

User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Trouble with StringReplace * ? ;

Post by PuzzledGreatly » 13 Oct 2013, 11:04

Tyrer wrote:The code doesn't work because in the first stringreplace, var1 becomes var with * replaced.
Arh, thanks, I had missed that.

User avatar
dangerdogL2121
Posts: 173
Joined: 01 Oct 2013, 23:11

Re: Trouble with StringReplace * ? ;

Post by dangerdogL2121 » 14 Oct 2013, 00:12

Hey guys

This saves a static variable

Code: Select all

var=*123*?

Test(Var)
{
  Stringreplace, var1, var, *, %A_space%, All
  Stringreplace, var1, var1, ?, %A_space%, All
  msgbox %var1%
return var1
}
returnvalue := Test(var)
msgbox, %returnvalue%
Don't know if this is better or worse than a static or global variable but its just another way to do it. ;)

Post Reply

Return to “Ask for Help (v1)”