AppleScript 中的字符串操作
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也会使用偏移量。
“xyz”的文本 1 到 2
相当于字符串形式的“xyz”的项目 1 到 2。据我所知,您不必恢复文本项目分隔符。
如果其他人正在搜索一般的字符串操作,这里有替换和分割文本以及连接列表的方法:
字符串比较默认忽略大小写:
反转文本:
您可以使用 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 %s
。LC_CTYPE=UTF-8
使字符类包含一些非 ASCII 字符。 如果省略不改变行结尾
,则换行符将替换为回车符,并且输出末尾的换行符将被删除。paragraphs of
将字符串围绕 \n、\r 和 \r\n 分割。 它不会删除分隔符。剪贴板的纯文本版本使用 CR 行结尾。 这会将行结尾转换为 LF:
自 10.5:
I'd also use offsets.
text 1 thru 2 of "xyz"
is equivalent toitems 1 thru 2 of "xyz" as string
.As far as I know, you don't have to restore text item delimiters.
If others were searching about string manipulation in general, here are methods for replacing and splitting text and joining lists:
String comparisons ignore case by default:
Reversing 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 ofprintf %s
.LC_CTYPE=UTF-8
makes character classes include some non-ASCII characters. Ifwithout 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.The plain text version of the clipboard uses CR line endings. This converts line endings to LF:
Unicode text
has been equivalent withtext
andstring
since 10.5:您可能想使用更简单的解决方案,如下所示:
You may want to use a simpler solution like this: