Skip to content

Search a String: Offset

There are a lot of handy string functions in most languages. You can find a list of equivalents for various Python string functions here. One commonly used string function is offset, which can find a string. If you are searching for the word “needle” in a string variable called “haystack” then doing this:

offset of "needle" in haystack

will return the start position of “needle” and if it doesn’t find needle at all, will return 0.

4 Comments

  1. has wrote:

    Text manipulation is definitely not one of AppleScript’s strong points, and its out-of-the-box support for it really sucks. However, if you’re working in Studio you can easily take advantage of Cocoa’s own NSString methods and third-party text processing frameworks such as RegexKit. For example, to uppercase an AppleScript string:

    set myText to call method “uppercaseString” of myText

    For non-Studio-based projects, your best bet would be to use a Unicode-aware scriptable text editor, e.g. TextWrangler, or scripting addition/scriptable faceless background application, e.g. TextCommands.

    HTH

    Tuesday, May 20, 2008 at 5:47 am | Permalink
  2. K. M. Lawson wrote:

    I will have to look into some more of thos NSString methods within Cocoa! Thanks for passing that info along!

    Tuesday, May 20, 2008 at 9:26 am | Permalink
  3. K. M. Lawson wrote:

    Intersting, I can’t seem to get that call method command to compile in a test application.

    Tuesday, May 20, 2008 at 8:17 pm | Permalink
  4. has wrote:

    “I can’t seem to get that call method command to compile in a test application.”

    If cutting and pasting, beware WordPress’s curly quotes. AppleScript requires straight double quotes for string literals.

    Saturday, May 24, 2008 at 4:03 am | Permalink