Page 1 of 1

StrReplace(string, chars)

Posted: 12 Dec 2013, 07:41
by Holle
This Script replace a lot of chars/strings in a string.
Because the "chars" always must be pairs, the function StdErr_Write() is needed to make sure that only "pairs" are specified.

Code: Select all

; ============================================================================================
; | StrReplace(string, chars)
; |
; | Date    :   2013 / 12 / 12
; |
; | Author  :   Holle
; |
; | Syntax: 	StrReplace("Text", "replace, with, replace, with, ...")
; |
; | Examples: 	StrReplace("Hello", "H,B,o,a") --> "Bella"
; |				StrReplace("Abcde123", "bcd,v,1," A_Space ",23,Cesar") --> "Ave Cesar"
; |
; ============================================================================================
StrReplace(string, chars) {
	StringSplit, char, chars, `,
	if mod(char0,2) 
		ExitApp StdErr_Write(A_LineNumber,"This function needs pairs of parameter.","odd number (" strings.MaxIndex() ")")
	ParNum := 1
	ParNum2 := ParNum +1
	while(ParNum < char0) {
		with := ParNum +1 ,	replace := Char%ParNum% , with := Char%with%
		StringReplace, string, string, %replace%, %with%, all
		ParNum += 2
	}
return string
}

Re: StrReplace(string, chars)

Posted: 12 Dec 2013, 08:14
by Chef
Author not Autor.

Re: StrReplace(string, chars)

Posted: 12 Dec 2013, 08:15
by Holle
Chef wrote:Author not Autor.
:oops: Oops ...many thanks Chef

Re: StrReplace(string, chars)

Posted: 12 Dec 2013, 09:14
by Chef
Your welcome, and thanks for posting these useful functions Image

Re: StrReplace(string, chars)

Posted: 12 Dec 2013, 09:38
by joedf
another nice one :)

Re: StrReplace(string, chars)

Posted: 12 Dec 2013, 11:10
by Guest10
is there a function to find and replace a string with another string within ALL .txt files and .ahk files within a folder?

Re: StrReplace(string, chars)

Posted: 12 Dec 2013, 11:22
by joedf
something like this? i whipped up this real quick :D Caution! untested :o

Code: Select all

file_sreplace(SEARCHTEXT,REPLACETEXT="",ext=txt) {
    Loop, %A_ProgramFiles%\*.%ext%, , 1  ; Recurse into subfolders.
    {
        FileRead, temp, %A_LoopFileFullPath%
        StringReplace, temp, temp, %SEARCHTEXT%, %REPLACETEXT%,All
        FileDelete,%A_LoopFileFullPath%
        FileAppend,%temp%,%A_LoopFileFullPath%
    }
}

Re: StrReplace(string, chars)

Posted: 12 Dec 2013, 11:34
by Guest10
is it FileDelete, %A_LoopFileFullPath% or FileDelete, temp, %A_LoopFileFullPath%?

Re: StrReplace(string, chars)

Posted: 12 Dec 2013, 11:35
by joedf
FileDelete,%A_LoopFileFullPath% ;)

Re: StrReplace(string, chars)

Posted: 12 Dec 2013, 12:03
by Guest10
thanks, does this also work with .ahk files?

Re: StrReplace(string, chars)

Posted: 12 Dec 2013, 12:05
by joedf
It should :) Since they are also "text" files :)

Re: StrReplace(string, chars)

Posted: 12 Dec 2013, 17:54
by AfterLemon
I wrote a version using Arrays and Associative-array input {}.

Using AHK_L this should work better/faster:

Code: Select all

Msgbox,% StrReplace2("Hello",{H:"B",o:"a"}) ; --> "Bella"
Msgbox,% StrReplace2("Abcde123",{bcd:"v",1:" ",23:"Cesar"}) ; --> "Ave Cesar"

StrReplace2(string,chars*){
	For k,c in (chars.MaxIndex()=1?chars[1]:chars)
		StringReplace,string,string,%k%,%c%,1
return string
}

Re: StrReplace(string, chars)

Posted: 12 Dec 2013, 18:13
by Guest10
i am again confused on the issue of AHK_L and version 1.1.13.01 etc. ... :( :ugeek:

Re: StrReplace(string, chars)

Posted: 12 Dec 2013, 19:01
by FanaticGuru
Guest10 wrote:i am again confused on the issue of AHK_L and version 1.1.13.01 etc. ... :( :ugeek:
AHK stands for AutoHotkey.

There use to be AHK.

Then came AHK_L.

AHK is pretty much called AHK Basic or something along that line now.

AHK_L has pretty much become the standard and is often now just called AHK.

Version 1.1.13.01 is AHK_L. Really at this point most anytime people talk about AHK they are talking about AHK_L which is now the standard version.

You got to go out of your way to still be using AHK Basic. I rarely see any discussion or scripts specifically for AHK Basic unless someone is trying to convert very old scripts to work with the new version of AHK.

Then within AHK_L you have ANSI 32 bit. Obsolete and not very commonly discussed except for converting to newer tech.

AHK_L Unicode 32 bit. The most common and standard flavor. Will run on Windows 32 bit or Windows 64 bit. This is what people are steered toward to download and install.

AHK_L Unicode 64 bit. Designed for 64 bit operating system. Is still a little before its time. Faster but too many people still use Unicode 32 bit for it to be widely adopted.

You are fairly safe to assume any talk about AHK is actually talk about AHK_L Unicode 32 bit the newest version you can down load.

That is my take anyways.

FG

Re: StrReplace(string, chars)

Posted: 12 Dec 2013, 19:01
by joedf
actually the term "ahk_L" should no longer be used... :P it's either simply ahk or ahk v1.1

Re: StrReplace(string, chars)

Posted: 12 Dec 2013, 19:24
by FanaticGuru
joedf wrote:actually the term "ahk_L" should no longer be used... :P it's either simply ahk or ahk v1.1
I agree. It causes confusion.

I believe it should just be AHK and if you need to refer to the old one it be called AHK Basic.

FG

Re: StrReplace(string, chars)

Posted: 13 Dec 2013, 00:29
by Holle
AfterLemon wrote:I wrote a version using Arrays and Associative-array input {}.

Using AHK_L this should work better/faster:

Code: Select all

Msgbox,% StrReplace2("Hello",{H:"B",o:"a"}) ; --> "Bella"
Msgbox,% StrReplace2("Abcde123",{bcd:"v",1:" ",23:"Cesar"}) ; --> "Ave Cesar"

StrReplace2(string,chars*){
	For k,c in (chars.MaxIndex()=1?chars[1]:chars)
		StringReplace,string,string,%k%,%c%,1
return string
}
Thank you, it´s very compactly. I will however use the other code, because it is easier to use.

Re: StrReplace(string, chars)

Posted: 13 Dec 2013, 00:34
by Holle
joedf wrote:something like this? i whipped up this real quick :D Caution! untested :o

Code: Select all

file_sreplace(SEARCHTEXT,REPLACETEXT="",ext=txt) {
    Loop, %A_ProgramFiles%\*.%ext%, , 1  ; Recurse into subfolders.
    {
        FileRead, temp, %A_LoopFileFullPath%
        StringReplace, temp, temp, %SEARCHTEXT%, %REPLACETEXT%,All
        FileDelete,%A_LoopFileFullPath%
        FileAppend,%temp%,%A_LoopFileFullPath%
    }
}
Looks good. Instead of

Code: Select all

StringReplace, temp, temp, %SEARCHTEXT%, %REPLACETEXT%,All
you can use "StrReplace()" to replace more chars/strings at once.

Re: StrReplace(string, chars)

Posted: 13 Dec 2013, 01:08
by AfterLemon
Holle, they are essentially identical in use except mine uses documented associative array style instead of your undocumented style of input. With larger arrays and replacement totals, the difference becomes even more pronounced. Just an FYI. What both of us should probably include is the ability to replace one item at a time as an option instead of assuming all instances replacement is desired.

EDIT:
Updated as I stated above. No other modifications.

Code: Select all

Msgbox,% StrReplace2("Hello",,{e:"u",l:"b",o:"a"}) ; --> "Hubla"
Msgbox,% StrReplace2("Hello",1,{e:"u",l:"b",o:"a"}) ; --> "Hubba"

StrReplace2(string,all:=0,chars*){
   For k,c in (chars.MaxIndex()=1?chars[1]:chars)
      StringReplace,string,string,%k%,%c%,%all%
return string
}
EDIT2:
As a followup, by default mine allows for number sequences to be replaced more easily:

Code: Select all

Msgbox,% StrReplace2("12345671234",,"u","b","a") ; --> "uba45671234"
Msgbox,% StrReplace2("12345671234",1,"u","b","a") ; --> "uba4567uba4"