Replace First Occurrence of a Substring in a String

Peter W A Wood peterwawood at gmail.com
Sun Sep 21 04:23:24 EDT 2014


There must be an easy way to replace only the first occurrence of a substring within a string. The replaceText function replaces all occurrences of a string which matches the the supplied substring (actually the supplied regular expression).

Obviously, it can be done with a loop, something like this:

 	put "Cat and Dog" into tString
  	repeat with tPos = 1 to the length of tString
      		if char tPos of tString = "a" then 
        		put "*" into char tPos of tString
        		exit repeat
      		end if
   	end repeat

If there isn't an easier way, I'm sure many of you know a better way.

Regards

Peter



More information about the use-livecode mailing list