AppleScript 中的字符串操作

String manipulation in AppleScript

提问人: 提问时间:7/15/2009 最后编辑:DᴀʀᴛʜVᴀᴅᴇʀ 更新时间:4/20/2017 访问量:24318

问:

我在 AppleScript 中面临的挑战是按如下方式操作字符串:

  • 基本字符串是电子邮件收件人显示名称,例如:First Last ([email protected])
  • 我想“修剪”显示名称以删除括号中的实际电子邮件地址
  • 所需的结果应该是 - 因此需要删除第一个支架前面的空间。First Last

在 AppleScript 中执行此操作的最佳和最有效的方法是什么?

字符串 Applescript 数据操作

评论


答:

3赞 Philip Regan 7/15/2009 #1
set theSample to "First Last ([email protected])"

return trimEmailAddress(theSample)
-->Result: "First Last"

on trimEmailAddress(sourceAddress)
    set AppleScript's text item delimiters to {" ("}
    set addressParts to (every text item in sourceAddress) as list
    set AppleScript's text item delimiters to ""
    set nameOnly to item 1 of addressParts

    return nameOnly
end trimEmailAddress

评论

1赞 mklement0 9/7/2012
很棒的解决方案。仅名称部分的提取可以简化为 ,省去了涉及 的辅助语句的需要。set nameOnly to first text item of sourceAddressaddressParts
1赞 Zitoun 4/19/2013 #2

您可能希望使用更简单的解决方案,如下所示:

set theSample to "First Last ([email protected])"

on trimEmailAddress(sourceAddress)
    set cutPosition to (offset of " (" in sourceAddress) - 1
    return text 1 thru cutPosition of sourceAddress
end trimEmailAddress

return trimEmailAddress(theSample)
-->  "First Last"

评论

0赞 Zitoun 4/22/2013
> “xyz” 的文本 1 到 2 等同于字符串 “xyz” 的第 1 到 2 项 Oh!谢谢Lauri的提示,不知道这个!
5赞 Lri 4/20/2013 #3

我也会使用偏移量。 等效于 。text 1 thru 2 of "xyz"items 1 thru 2 of "xyz" as string

set x to "First Last ([email protected])"
set pos to offset of " (" in x
{text 1 thru (pos - 1) of x, text (pos + 2) thru -2 of x}

据我所知,您不必恢复文本项分隔符

set x to "First Last ([email protected])"
set text item delimiters to {" (", ")"}
set {fullname, email} to text items 1 thru 2 of x

如果其他人一般都在搜索字符串操作,以下是替换和拆分文本和联接列表的方法:

on replace(input, x, y)
    set text item delimiters to x
    set ti to text items of input
    set text item delimiters to y
    ti as text
end replace

on split(input, x)
    if input does not contain x then return {input}
    set text item delimiters to x
    text items of input
end split

on join(input, x)
    set text item delimiters to x
    input as text
end join

默认情况下,字符串比较会忽略大小写:

"A" is "a" -- true
"ab" starts with "A" -- true
considering case
    "A" is "a" -- false
    "ab" starts with "A" -- false
end considering

反转文本:

reverse of items of "esrever" as text

你可以使用 do shell 脚本来改变文本的大小写:

do shell script "printf %s " & quoted form of "aä" & " | LC_CTYPE=UTF-8 tr [:lower:] [:upper:]" without altering line endings

默认情况下,echo 在 OS X 的 /bin/sh 中解释转义序列。您也可以使用 代替 . 使字符类包含一些非 ASCII 字符。如果省略,则换行符将替换为回车符,并删除输出末尾的换行符。shopt -u xpg_echo; echo -nprintf %sLC_CTYPE=UTF-8without altering line endings

paragraphs of拆分 \n、\r 和 \r\n 周围的字符串。它不会剥离分隔符。

paragraphs of ("a" & linefeed & "b" & return & "c" & linefeed)
-- {"a", "b", "c", ""}

剪贴板的纯文本版本使用 CR 行结尾。这会将行尾转换为 LF:

set text item delimiters to linefeed
(paragraphs of (get the clipboard as text)) as text

Unicode text10.5 等效,自 10.5 起:textstring

Unicode 和非 Unicode 文本之间不再有区别。正好有一个文本类,名为“text”:也就是说,“foo”的类返回文本。