StrReplace(string, chars)

Post your working scripts, libraries and tools
User avatar
Holle
Posts: 186
Joined: 01 Oct 2013, 23:19

StrReplace(string, chars)

12 Dec 2013, 07:41

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
}
Last edited by Holle on 12 Dec 2013, 08:15, edited 1 time in total.
User avatar
Chef
Posts: 50
Joined: 14 Nov 2013, 13:01

Re: StrReplace(string, chars)

12 Dec 2013, 08:14

Author not Autor.
User avatar
Holle
Posts: 186
Joined: 01 Oct 2013, 23:19

Re: StrReplace(string, chars)

12 Dec 2013, 08:15

Chef wrote:Author not Autor.
:oops: Oops ...many thanks Chef
User avatar
Chef
Posts: 50
Joined: 14 Nov 2013, 13:01

Re: StrReplace(string, chars)

12 Dec 2013, 09:14

Your welcome, and thanks for posting these useful functions Image
User avatar
joedf
Posts: 7357
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: StrReplace(string, chars)

12 Dec 2013, 09:38

another nice one :)
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500 @ 4.00 GHz, 2x8GB G.Skill RipJaws V - DDR4 3280 MHz, NVIDIA GTX 1060 6GB | [About Me] | [ASPDM - StdLib Distribution]
[Populate the AHK MiniCity!] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library] | [About the AHK Foundation]
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: StrReplace(string, chars)

12 Dec 2013, 11:10

is there a function to find and replace a string with another string within ALL .txt files and .ahk files within a folder?
User avatar
joedf
Posts: 7357
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: StrReplace(string, chars)

12 Dec 2013, 11:22

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%
    }
}
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500 @ 4.00 GHz, 2x8GB G.Skill RipJaws V - DDR4 3280 MHz, NVIDIA GTX 1060 6GB | [About Me] | [ASPDM - StdLib Distribution]
[Populate the AHK MiniCity!] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library] | [About the AHK Foundation]
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: StrReplace(string, chars)

12 Dec 2013, 11:34

is it FileDelete, %A_LoopFileFullPath% or FileDelete, temp, %A_LoopFileFullPath%?
User avatar
joedf
Posts: 7357
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: StrReplace(string, chars)

12 Dec 2013, 11:35

FileDelete,%A_LoopFileFullPath% ;)
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500 @ 4.00 GHz, 2x8GB G.Skill RipJaws V - DDR4 3280 MHz, NVIDIA GTX 1060 6GB | [About Me] | [ASPDM - StdLib Distribution]
[Populate the AHK MiniCity!] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library] | [About the AHK Foundation]
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: StrReplace(string, chars)

12 Dec 2013, 12:03

thanks, does this also work with .ahk files?
User avatar
joedf
Posts: 7357
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: StrReplace(string, chars)

12 Dec 2013, 12:05

It should :) Since they are also "text" files :)
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500 @ 4.00 GHz, 2x8GB G.Skill RipJaws V - DDR4 3280 MHz, NVIDIA GTX 1060 6GB | [About Me] | [ASPDM - StdLib Distribution]
[Populate the AHK MiniCity!] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library] | [About the AHK Foundation]
User avatar
AfterLemon
Posts: 83
Joined: 30 Sep 2013, 14:27
Location: Arizona, USA

Re: StrReplace(string, chars)

12 Dec 2013, 17:54

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
}
HOME: Windows 7 x64 SP1 Ultimate | AMD FX 8350 8-Core @ 4.00GHZ | 16GB Ram
WORK: Windows 7 x64 SP1 Home Premium | AMD Athlon II X4 640 @ 3.00GHz | 8GB Ram
MOBILE: Samsung Galaxy Note 3 | 2012 Asus Nexus 7 | Zune 80Gb
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: StrReplace(string, chars)

12 Dec 2013, 18:13

i am again confused on the issue of AHK_L and version 1.1.13.01 etc. ... :( :ugeek:
FanaticGuru
Posts: 1363
Joined: 30 Sep 2013, 22:25

Re: StrReplace(string, chars)

12 Dec 2013, 19:01

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
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
User avatar
joedf
Posts: 7357
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: StrReplace(string, chars)

12 Dec 2013, 19:01

actually the term "ahk_L" should no longer be used... :P it's either simply ahk or ahk v1.1
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500 @ 4.00 GHz, 2x8GB G.Skill RipJaws V - DDR4 3280 MHz, NVIDIA GTX 1060 6GB | [About Me] | [ASPDM - StdLib Distribution]
[Populate the AHK MiniCity!] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library] | [About the AHK Foundation]
FanaticGuru
Posts: 1363
Joined: 30 Sep 2013, 22:25

Re: StrReplace(string, chars)

12 Dec 2013, 19:24

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
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
User avatar
Holle
Posts: 186
Joined: 01 Oct 2013, 23:19

Re: StrReplace(string, chars)

13 Dec 2013, 00:29

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.
User avatar
Holle
Posts: 186
Joined: 01 Oct 2013, 23:19

Re: StrReplace(string, chars)

13 Dec 2013, 00:34

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.
User avatar
AfterLemon
Posts: 83
Joined: 30 Sep 2013, 14:27
Location: Arizona, USA

Re: StrReplace(string, chars)

13 Dec 2013, 01:08

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"
HOME: Windows 7 x64 SP1 Ultimate | AMD FX 8350 8-Core @ 4.00GHZ | 16GB Ram
WORK: Windows 7 x64 SP1 Home Premium | AMD Athlon II X4 640 @ 3.00GHz | 8GB Ram
MOBILE: Samsung Galaxy Note 3 | 2012 Asus Nexus 7 | Zune 80Gb

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Bon, Google [Bot] and 51 guests