Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Function: StringReplaceWord


  • Please log in to reply
4 replies to this topic
AGU
  • Guests
  • Last active:
  • Joined: --
I have written this small function and thought "Why not sharing it?" I don't know if it's very useful or makes much sense but I needed it in one of my scripts and didn't find any easier way to accomplish what I wanted.

It can replace a word in a string by passing the position of the word determinded with StringGetPos. Have a look:

StringReplaceWord(In_String,In_StartWordPos,In_WordLength,In_ReplaceText)
{
  ; In_String       : InputVar
  ; In_StartWordPos : Position of substring that will be replaced (determined with StringGetPos)
  ; In_WordLength   : Number of chars the substring consists of
  ; In_ReplaceText  : substring will be replaced with this text

  local Length, Before, After, Ret_Val
  StringLen, Length, In_String
  If (In_StartWordPos>Length)OR(In_StartWordPos+In_WordLength>Length)OR(Length=0)
     return, %In_String%
  StringLeft, Before, In_String, In_StartWordPos
  StringRight, After, In_String, Length-(In_StartWordPos+In_WordLength)
  Ret_Val=%Before%%In_ReplaceText%%After%   
  return, %Ret_Val%
}

Here's a short code snippet to demonstrate how to use it.

var = AGU is stupid
StringGetPos, pos, var, stupid
result := StringReplaceWord(var, pos, 6, "so brilliant")
MsgBox, Original:`t`t%var%`n`nAfter Function:`t%result%

____________
Regards
AGU a.k.a AGermanUser

Invalid User
  • Members
  • 447 posts
  • Last active: Mar 27 2012 01:04 PM
  • Joined: 14 Feb 2005
Couldnt you place the String get len and pos in the fucntion, I dont see why they are needed as paramaters. Just a thought. Perhaps add instance.
my lame sig :)

corrupt
  • Members
  • 2558 posts
  • Last active: Nov 01 2014 03:23 PM
  • Joined: 29 Dec 2004
Thanks for sharing :) . Here's a variation:
var = This is boring.

result := StringReplaceWord(var, "boring", "interesting..") 

MsgBox, Original:`t`t%var%`n`nAfter Function:`t%result%



var = This is a test  

result := StringReplaceWord(var, "is", "was", "All") 

MsgBox, Original:`t`t%var%`n`nAfter Function:`t%result%`n`nHmm... additional code needed to detect words...



var = This is a test  

result := StringReplaceWord(var, "is", "was", "", "4") 

MsgBox, Original:`t`t%var%`n`nAfter Function:`t%result%`n`nOr specify start position...







StringReplaceWord(In_String, In_searchword, In_ReplaceText, sr_ReplaceAll=0, In_StartPos=0)

{

  StringGetPos, sr_pos, In_String, %In_searchword%,, %In_StartPos%

  If ErrorLevel

    Return, In_String

  sr_right_o := (StrLen(In_String) - sr_pos)

  StringRight, sr_right_o, In_String, sr_right_o

  StringReplace, sr_right_n, sr_right_o, %In_searchword%, %In_ReplaceText%, %sr_ReplaceAll%

  StringReplace, In_String, In_String, %sr_right_o%, %sr_right_n%

  Return, In_String

}


AGU
  • Guests
  • Last active:
  • Joined: --
Nice idea about integrating the StringGetPos command. But I need the L#|R# option of StringGetPos. How would I accomplish that?
There is no default value for this option. By default this parameter is omitted.
e.g. I can't define In_Occurance=0 like In_StartPos=0.

Any idea or am I missing s.th?

corrupt
  • Members
  • 2558 posts
  • Last active: Nov 01 2014 03:23 PM
  • Joined: 29 Dec 2004
var = This is a test  

result := StringReplaceWord(var, "is", "was", "", "R1") 

MsgBox, Original:`t`t%var%`n`nAfter Function:`t%result%`n`n



; Modified In_StartPos param:

; If In_StartPos contains L or R then L# or R# will be used.

; Otherwise In_StartPos is assumed to be an offset value (default = 0)



StringReplaceWord(In_String, In_searchword, In_ReplaceText, sr_ReplaceAll=0, In_StartPos=0) 

{ 

  If In_StartPos contains L,R

  {

    In_DoLR := In_StartPos

    In_StartPos=

  }

  StringGetPos, sr_pos, In_String, %In_searchword%, %In_DoLR%, %In_StartPos%

  If ErrorLevel 

    Return, In_String 

  sr_right_o := (StrLen(In_String) - sr_pos) 

  StringRight, sr_right_o, In_String, sr_right_o 

  StringReplace, sr_right_n, sr_right_o, %In_searchword%, %In_ReplaceText%, %sr_ReplaceAll% 

  StringReplace, In_String, In_String, %sr_right_o%, %sr_right_n% 

  Return, In_String 

}

:)