如何去掉 AppleScript 中字符串的特定部分?
How do I get rid of specific parts of a string in AppleScript?
这里是有问题的字符串:
xxx:父目录:子目录:file.fl
我想去掉所有以第一个冒号“:”开头直到字符串结尾的字符,这样我只剩下“xxx”
xxx 的字符数因我的输入而异。
这是一个别名路径,我只想要它的第一位。
您可以使用AppleScript's text item delimiters
来完成:
set foo to "xxx:parentdir:childdir:file.fl"
set AppleScript's text item delimiters to ":"
set foo to first text item of foo as string
set AppleScript's text item delimiters to ""
return foo
结果:“xxx”
AppleScript 字符串中的字符有一个索引或偏移量,相对于字符串的开始 (1) 或结束 (-1),文本可以使用具有开始和结束索引的范围指定。对于您的示例,您可以搜索所需字符或文本的偏移量,并将其用作结束位置:
set testingText to (choose file) as text
set here to (offset of ":" in testingText) - 1 -- don't include the character
display dialog text 1 thru here of testingText
这里是有问题的字符串:
xxx:父目录:子目录:file.fl
我想去掉所有以第一个冒号“:”开头直到字符串结尾的字符,这样我只剩下“xxx”
xxx 的字符数因我的输入而异。
这是一个别名路径,我只想要它的第一位。
您可以使用AppleScript's text item delimiters
来完成:
set foo to "xxx:parentdir:childdir:file.fl"
set AppleScript's text item delimiters to ":"
set foo to first text item of foo as string
set AppleScript's text item delimiters to ""
return foo
结果:“xxx”
AppleScript 字符串中的字符有一个索引或偏移量,相对于字符串的开始 (1) 或结束 (-1),文本可以使用具有开始和结束索引的范围指定。对于您的示例,您可以搜索所需字符或文本的偏移量,并将其用作结束位置:
set testingText to (choose file) as text
set here to (offset of ":" in testingText) - 1 -- don't include the character
display dialog text 1 thru here of testingText