Dieuz Posted March 15, 2010 Share Posted March 15, 2010 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 ! Link to comment Share on other sites More sharing options...
Developers Jos Posted March 15, 2010 Developers Share Posted March 15, 2010 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? SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Fulano Posted March 15, 2010 Share Posted March 15, 2010 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! Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted March 15, 2010 Moderators Share Posted March 15, 2010 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. Link to comment Share on other sites More sharing options...
Dieuz Posted March 15, 2010 Author Share Posted March 15, 2010 (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 March 15, 2010 by Dieuz Link to comment Share on other sites More sharing options...
Developers Jos Posted March 15, 2010 Developers Share Posted March 15, 2010 (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 March 15, 2010 by Jos 232showtime 1 SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Dieuz Posted March 15, 2010 Author Share Posted March 15, 2010 (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 March 15, 2010 by Dieuz Link to comment Share on other sites More sharing options...
Fulano Posted March 15, 2010 Share Posted March 15, 2010 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! Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted March 15, 2010 Moderators Share Posted March 15, 2010 $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. Link to comment Share on other sites More sharing options...
Fulano Posted March 15, 2010 Share Posted March 15, 2010 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! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now