AppleScript:字符串中子字符串的索引

发布于 2024-08-11 01:15:29 字数 255 浏览 9 评论 0原文

我想创建一个函数,返回特定字符串的子字符串,从该字符串的开头一直到但不包括另一个特定字符串的开头。有想法吗?


因此,

substrUpTo(theStr, subStr)

如果我输入 substrUpTo("Today is mybirth", "my"),它将返回第一个参数的子字符串,直到但不包括第二个参数开始的位置。 (即它会返回“今天是”

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 ")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如日中天 2024-08-18 01:15:29
set s to "Today is my birthday"
set AppleScript's text item delimiters to "my"
text item 1 of s
--> "Today is "
set s to "Today is my birthday"
set AppleScript's text item delimiters to "my"
text item 1 of s
--> "Today is "
迷鸟归林 2024-08-18 01:15:29

内置 offset 命令应该执行此操作:

set s to "Today is my birthday"
log text 1 thru ((offset of "my" in s) - 1) of s
--> "Today is "

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 "
ㄟ。诗瑗 2024-08-18 01:15:29

可能有点笨拙,但它完成了工作......

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

Probably a bit kludgey, but it gets the job done...

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文