The <() contains ()?> block can check whether a string is inside another string, but no block exists that returns the location of a substring within a string. This tutorial shows an algorithm that can do just this.

An example implementation can be found here.

Variables

The script uses two variables:

  • (buffer) stores a growing copy of the original string.
  • (output) is the index where the substring is located. If it isn't found, (output) is set to 0.

Script

define find (substring) in (string)
if <(string) contains (substring)?> then
    set [buffer v] to []
    repeat until <(buffer) contains (substring)?>
        set [buffer v] to (join (buffer) (letter ((length of (buffer)) + (1)) of (string)))
    end
    set [output v] to ((length of (buffer)) - (length of (substring)))
else
    set [output v] to [0]
end
Cookies help us deliver our services. By using our services, you agree to our use of cookies.