String matching: Difference between revisions

Added solution for AppleScript
(Added solution for AppleScript)
Line 151:
TFFT -1 +1 +3
</pre>
 
=={{header|AppleScript}}==
<lang AppleScript>set stringA to "I felt happy because I saw the others were happy and because I knew I should feel happy, but I wasn’t really happy."
 
set string1 to "I felt happy"
set string2 to "I should feel happy"
set string3 to "I wasn't really happy"
 
-- Determining if the first string starts with second string
stringA starts with string1 --> true
 
-- Determining if the first string contains the second string at any location
stringA contains string2 --> true
 
-- Determining if the first string ends with the second string
stringA ends with string3 --> false
 
-- Print the location of the match for part 2
offset of string2 in stringA --> 69</lang>
AppleScript doesn't have a builtin means of matching multiple occurrences of a substring, however one can redefine the existing '''offset''' command to add this functionality:
<lang AppleScript>-- Handle multiple occurrences of a string for part 2
on offset of needle in haystack
local needle, haystack
if the needle is not in the haystack then return {}
set my text item delimiters to the needle
script
property N : needle's length
property t : {1 - N} & haystack's text items
end script
tell the result
repeat with i from 2 to (its t's length) - 1
set x to item i of its t
set y to item (i - 1) of its t
set item i of its t to (its N) + (x's length) + y
end repeat
items 2 thru -2 of its t
end tell
end offset
 
offset of "happy" in stringA --> {8, 44, 83, 110}</lang>
 
=={{header|AutoHotkey}}==
Anonymous user