简体   繁体   中英

What am I doing wrong here? (AppleScript remove multiple parts of string)

I'm trying to clean up the string so that it removes "//name@" and "deletethispart.file" from "//name@main.domain.com/directory01/directory02/deletethispart.file"

set mainString to "//name@main.domain.com/directory01/directory02/deletethispart.file"
set cleanUpTerms to {"//name@", "deletethispart.file"}
repeat with i from 1 to count of cleanUpTerms
    set text item delimiters to item i of cleanUpTerms
    set cleanURL to text items of mainString
    set text item delimiters to ""
    set mainString to cleanURL
end repeat

It's not working:(

You have to concatenate the text items at the end of the repeat loop.

Renaming cleanURL with textItems makes it clearer

set mainString to "//name@main.domain.com/directory01/directory02/deletethispart.file"
set cleanUpTerms to {"//name@", "deletethispart.file"}
repeat with i from 1 to count of cleanUpTerms
    set text item delimiters to item i of cleanUpTerms
    set textItems to text items of mainString
    set text item delimiters to ""
    set mainString to textItems as text
end repeat

As red_menace said, text item delimiters can be a list. it means you do not need a loop for each delimiter:

set mainString to "//name@main.domain.com/directory01/directory02/deletethispart.file"
set cleanUpTerms to {"//name@", "deletethispart.file"}
set text item delimiters to cleanUpTerms

set cleanURL to text items of mainString
set text item delimiters to ""
set mainString to cleanURL as text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM