AppleScript 中的字符串操作

发布于 2024-07-27 05:10:20 字数 382 浏览 2 评论 0原文

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

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

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

I have the challenge in AppleScript to manipulate a string as follows:

  • Base string is an email recipient display name, say: First Last ([email protected])
  • I'd like to "trim" the display name to remove the actual email address in the brackets
  • The desired result should be First Last - so the space in front of the first bracket needs to be removed.

What is the best and most efficient way to do this in AppleScript?

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

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

发布评论

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

评论(3

迷爱 2024-08-03 05:10:21

我也会使用偏移量。 “xyz”的文本 1 到 2 相当于字符串形式的“xyz”的项目 1 到 2。

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脚本“printf %s”& "aä" & 的引用形式 " | LC_CTYPE=UTF-8 tr [:lower:] [:upper:]" 不改变行结尾

echo 在 OS X 的 /bin/sh 中默认解释转义序列。 您还可以使用shopt -u xpg_echo; echo -n 而不是 printf %sLC_CTYPE=UTF-8 使字符类包含一些非 ASCII 字符。 如果省略不改变行结尾,则换行符将替换为回车符,并且输出末尾的换行符将被删除。

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

10.5:

Unicode 和非 Unicode 文本之间不再有区别。 只有一个文本类,名为“text”:即“foo”类返回文本。

I'd also use offsets. text 1 thru 2 of "xyz" is equivalent to 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}

As far as I know, you don't have to restore text item delimiters.

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

If others were searching about string manipulation in general, here are methods for replacing and splitting text and joining lists:

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

String comparisons ignore case by default:

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

Reversing text:

reverse of items of "esrever" as text

You can use do shell script to change the case of text:

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

echo interprets escape sequences by default in OS X's /bin/sh. You could also use shopt -u xpg_echo; echo -n instead of printf %s. LC_CTYPE=UTF-8 makes character classes include some non-ASCII characters. If without altering line endings is left out, linefeeds are replaced with carriage returns and a newline at the end of the output is removed.

paragraphs of splits strings around \n, \r, and \r\n. It doesn't strip delimiters.

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

The plain text version of the clipboard uses CR line endings. This converts line endings to LF:

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

Unicode text has been equivalent with text and string since 10.5:

There is no longer a distinction between Unicode and non-Unicode text. There is exactly one text class, named “text”: that is, class of "foo" returns text.

许仙没带伞 2024-08-03 05:10:21
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
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
清引 2024-08-03 05:10:21

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

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"

You may want to use a simpler solution like this:

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