Efficient Way To Check If Certain Characters Exist in Variable

Jeff Massung massung at gmail.com
Wed Jan 19 13:40:26 EST 2011


On Wed, Jan 19, 2011 at 11:34 AM, Warren Kuhl <warrenkuhl at gmail.com> wrote:

> What would be the most efficient way to check a variable and replace any of
> the characters with empty if the character is not a alpha (upper or lower),
> numeric (0-9) or a '-'?
>
> The only way I can think of doing this is a repeat loop and checking the
> value of each character and replacing with empty if not alpha, numeric or
> '-'.
>
> Any other suggestions for more efficient code?
>
>
Well, it depends on what your definition of "efficient" is. If you want a
1-liner:

put replaceText(tSource, "[^A-Za-z0-9\-]+", empty) into tDest

That's using regular expression. But that might be a lot less performance
(speed-wise) than doing it yourself in a loop:

repeat for each char c in tSource
   if c is in "abcdef...WXYZ0123456789-" then
      put c after tDest
   end if
end repeat

HTH,

Jeff M.



More information about the use-livecode mailing list