Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppleScript: Index of substring in string

I want to create a function that returns a substring of a specific string from the beginning of said string up to but not including the start of another specific string. Ideas?


So something like:

substrUpTo(theStr, subStr)

so if I inputted substrUpTo("Today is my birthday", "my"), it would return a substring of the first argument up to but not including where the second argument begins. (i.e. it would return "Today is ")

like image 822
Alexsander Akers Avatar asked Nov 11 '09 16:11

Alexsander Akers


2 Answers

set s to "Today is my birthday"
set AppleScript's text item delimiters to "my"
text item 1 of s
--> "Today is "
like image 156
has Avatar answered Sep 29 '22 17:09

has


The built-in offset command should do it:

set s to "Today is my birthday"
log text 1 thru ((offset of "my" in s) - 1) of s
--> "Today is "
like image 38
duozmo Avatar answered Sep 29 '22 15:09

duozmo