Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Replace Function


  • Please log in to reply
No replies to this topic
Jon
  • Members
  • 349 posts
  • Last active: Aug 30 2011 08:35 PM
  • Joined: 28 Apr 2004
The following function provides the same functions as the built in replace command but also allows you to specify a starting position (offset) so you can skip over (ignore) a number of characters from the left. It also allows you to specify the number of replacements to make and whether you want to start making these replacements from the left or the right of the string.

I remembered (after seeing Titans InstrX function) this topic that was posted a while ago and it occured to me that this could now be done with the use of a function.


Usage


Replace(string,find,replacewith[,start[,L#|R#]])


Parameters

string

Required. The string to be searched

find

Required. The part of the string that will be replaced

replacewith

Required. The replacement substring

start

Optional. Specifies the start position from the left of the string
(Number of characters to be skipped over.) Default is 0

L#|R#

Optional. Specifies the number of substitutions to perform.
Omit to make all possible substitutions
Determines whether replacing starts from the left or right of the string


ErrorLevel

contains the number of replacements made

Example

Msgbox, % Replace("A cat sat on the mat", "sat", "lay", 6,"L1")

;---------------------------------

string = abc 123 abc 123 abc 123 abc 123 abc 123 abc 123 abc 123

Msgbox, % Replace(string, "abc", "def", 8,"L3") . " (" . Errorlevel . " Replacements made)"

;returns: abc 123 def 123 def 123 def 123 abc 123 abc 123 abc 123 (3 Replacements made)

Msgbox, % Replace(string, "abc", "def", 0,"R2") . " (" . Errorlevel . " Replacements made)"

;returns: abc 123 abc 123 abc 123 abc 123 abc 123 def 123 def 123 (2 Replacements made)

Msgbox, % Replace(string, "abc", "def") . " (" . Errorlevel . " Replacements made)"

;returns: def 123 def 123 def 123 def 123 def 123 def 123 def 123 (7 Replacements made)


Function

Replace(string,find,replacewith,offset=0,LR#="")
{

  StringLeft, direction, LR#, 1
  StringTrimLeft, count, LR#, 1

  StringLeft,      start, string, %offset%
  StringTrimLeft,  end,   string, %offset%

  if direction = R
  {
    StringGetPos,  pos,   end,	%find%, R%count%
    StringRight,   right, end,	% StrLen(end)-pos
    StringReplace, right, right,%find%, %replacewith%, UseErrorLevel All
    StringLeft,    left,  end,	%pos%
    string=%start%%left%%right%
  }

  if direction = L
  {
    StringGetPos,  pos,	  end,	%find%, L%count%
    StringLeft,    left,  end,	% pos + StrLen(find)
    StringReplace, left,  left,	%find%, %replacewith%, UseErrorLevel All
    StringRight,   right, end,	% StrLen(end) - pos - StrLen(find)
    string=%start%%left%%right%
  }
    
  if LR# =
    StringReplace, string, string, %find%, %replacewith%, UseErrorLevel All

  return string
}