Sign in to follow this  
Followers 0
Dieuz

StringReplace - Multiple Substring at the same time

10 posts in this topic

Is there any way to use the StringReplace function (or any similar function), to replace multiple substring in a string at the same time?

Thanks !

Share this post


Link to post
Share on other sites



Is there any way to use the StringReplace function (or any similar function), to replace multiple substring in a string at the same time?

Thanks !

Not at the same time but what is wrong with doing them one after the other?

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

If you really must you can nestle them, but considering AutoIt is an interpreted language, fretting over the performance/memory differences isn't really worth the effort.


#fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!

Share this post


Link to post
Share on other sites

StringRegExpReplace() ?


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Not at the same time but what is wrong with doing them one after the other?

Well I have like 50 different Substrings to replace. I dont want to create 50 new variables.

Can I only use StringReplace without creating a new variable? It doesnt seem to work. See code below.

$string = "I love apple"
StringReplace($string, "apple", "banana")
StringReplace($string, "I", "We")
StringReplace($string, "love", "hate")
ConsoleWrite($string) ; Doesnt Output the correct result

I could so something like this but it would be WAY too long:

$string = "I love apple"
$string1= StringReplace($string, "apple", "banana")
$string2 = StringReplace($string1, "I", "We")
$string3= StringReplace($string2, "love", "hate")
ConsoleWrite($string3)
Edited by Dieuz

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

What's wrong with?:

$string = "I love apple"
$string = StringReplace($string, "apple", "banana")
$string = StringReplace($string, "I", "We")
$string = StringReplace($string, "hate", "We")
ConsoleWrite($string)

Or put the strings in an Array and loop through the array with a single StringReplace().

Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Wouldnt that look silly, with 50 time the same variable name.. ? :(

Anyway if it output the right result I guess it's allright!

Thanks!

Edited by Dieuz

Share this post


Link to post
Share on other sites

Or put the strings in an Array and loop through the array with a single StringReplace().

My favorite solution.


#fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!

Share this post


Link to post
Share on other sites

$string = "I love apple"
Global $a_rep[4][2] = [[3], ["apple", "bananas"], ["I", "We"], ["love", "hate"]]
For $i = 1 To $a_rep[0][0]
    $string = StringRegExpReplace($string, "\Q" & $a_rep[$i][0] & "\E", $a_rep[$i][1])
Next

ConsoleWrite($string & @CRLF)


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Slightly modified:

$string = "I love apple"
Local $a_rep[3][2] = [["apple", "bananas"], ["I", "We"], ["love", "hate"]]
For $i = 0 To Ubound($a_rep, 1) - 1
    $string = StringRegExpReplace($string, "\Q" & $a_rep[$i][0] & "\E", $a_rep[$i][1])
Next

ConsoleWrite($string & @CRLF)

Pretty much how I normally do it.

#fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0