Jump to content

Recommended Posts

Posted

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 !

  • Developers
Posted

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.
  :)

Posted

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!

  • Moderators
Posted

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.

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
  • Developers
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

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.
  :)

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
Posted

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!

  • Moderators
Posted

$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.

Posted

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!

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...