10

特定の文字列の先頭から別の特定の文字列の先頭までを含まない、特定の文字列のサブ文字列を返す関数を作成したいと思います。アイデア?


だから次のようなもの:

substrUpTo(theStr, subStr)

したがって、を入力substrUpTo("Today is my birthday", "my")すると、最初の引数のサブストリングが返されますが、2番目の引数の開始位置は含まれません。(つまり、戻り"Today is "ます)

4

3 に答える 3

13
set s to "Today is my birthday"
set AppleScript's text item delimiters to "my"
text item 1 of s
--> "Today is "
于 2009-11-15T10:47:54.683 に答える
9

組み込みoffsetコマンドはそれを行う必要があります:

set s to "Today is my birthday"
log text 1 thru ((offset of "my" in s) - 1) of s
--> "Today is "
于 2013-05-29T21:27:40.700 に答える
0

おそらく少し厄介ですが、それは仕事を成し遂げます...

property kSourceText : "Today is my birthday"
property kStopText : "my"

set newSubstring to SubstringUpToString(kSourceText, kStopText)

return newSubstring -- "Today is "

on SubstringUpToString(theString, subString) -- (theString as string, subString as string) as string

    if theString does not contain subString then
        return theString
    end if

    set theReturnString to ""

    set stringCharacterCount to (get count of characters in theString)
    set substringCharacterCount to (get count of characters in subString)
    set lastCharacter to stringCharacterCount - substringCharacterCount

    repeat with thisChar from 1 to lastCharacter
        set startChar to thisChar
        set endChar to (thisChar + substringCharacterCount) - 1
        set currentSubstring to (get characters startChar thru endChar of theString) as string
        if currentSubstring is subString then
            return (get characters 1 thru (thisChar - 1) of theString) as string
        end if
    end repeat

    return theString
end SubstringUpToString
于 2009-11-11T21:22:37.180 に答える