Position of a substring contained in a string?

Hello all,

I’m very new to this forum and this is my first posting.
I’m very new to AppleScript, too, so please be patient with me if I ask dull questions.
And sorry for my English – I’m not a native speaker (coming from Germany).

I searched the FAQ and the Forum for the keywords “position” and “substring” but
the results weren’t very satisfactory.

I’m writing an AppleScript Studio application that deals with iTunes and ID3 tags
and I need a subroutine/handler that’s capable of the following:
Given a string and a “separation pattern” (a string, too) I want as a result the
two strings that are contained in the original string but separated by the given
separation pattern.
Example:


   set theOriginalString to "Michael Jackson - One more chance"
   set theSepPat to " - "
   set theArtist to hereGoesMyMissingFunction(theOriginalString, theSepPat, 1)
   -- with 1 standing for the first substring. Expected result: theArtist = "Michael Jackson"
   set theTitle to hereGoesMyMissingFunction(theOriginalString, theSepPat, 2)
   -- with 2 standing for the second substring. Expected Result: theTitle = "One more chance"

The function should not be limited to just be able to handle one occurence of the
separation pattern. “Michael Jackson / Off the wall / Bad” would be another example
string. I think you get the point… :wink:

I could write such a handler if only I had some kind of “position” function. I know
that from other programming languages. It returns the position (as an integer)
in the original string (that is to be searched) of the first occurence of the search
pattern string. But I haven’t found such a function in AppleScript yet. Does it
exist? Or do I have to write it by myself? I just wanted to ask before I write it
and reinvent the wheel… :wink:
(One could write it with loops and text ranges and containment operators etc.)

Any help greatly appreciated! :slight_smile:

Greetings from Germany to all,
Stefan.

Model: PowerMac G5 2.7, PowerBook G4
Browser: Safari 412.2
Operating System: Mac OS X (10.4)

See AppleScript’s ‘text item delimiters’; crude, but will do what you want. Example:

set theOriginalString to "Michael Jackson - One more chance"
set text item delimiters to " - "
text items of theOriginalString --> {"Michael Jackson", "One more chance"}

WHOA!

Now, THAT’s cool!!!

:D:lol::cool:

Thanks A LOT! Saved a LOT of source code lines and complicated loops etc.!

Happily,
Stefan.