string AppleScript 中的字符串操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1129911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String manipulation in AppleScript
提问by Lri
I have the challenge in AppleScript to manipulate a string as follows:
我在 AppleScript 中遇到了如下操作字符串的挑战:
- 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.
- 基本字符串是电子邮件收件人的显示名称,例如:
First Last ([email protected])
- 我想“修剪”显示名称以删除括号中的实际电子邮件地址
- 期望的结果应该是
First Last
- 所以需要移除第一个支架前面的空间。
What is the best and most efficient way to do this in AppleScript?
在 AppleScript 中执行此操作的最佳和最有效方法是什么?
采纳答案by Philip Regan
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
回答by Lri
I'd also use offsets. text 1 thru 2 of "xyz"
is equivalent to items 1 thru 2 of "xyz" as string
.
我也会使用偏移量。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}
As far as I know, you don't have to restore text item delimiters.
据我所知,您不必恢复 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 来更改文本的大小写:
do shell script "printf %s " & quoted form of "a?" & " | LC_CTYPE=UTF-8 tr [:lower:] [:upper:]" without altering line endings
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.
echo 默认在 OS X 的 /bin/sh 中解释转义序列。您也可以使用shopt -u xpg_echo; echo -n
代替printf %s
. LC_CTYPE=UTF-8
使字符类包括一些非 ASCII 字符。如果without altering line endings
被省略,换行符将替换为回车,并删除输出末尾的换行符。
paragraphs of
splits strings around \n, \r, and \r\n. It doesn't strip delimiters.
paragraphs of
围绕\n、\r 和\r\n 拆分字符串。它不会去除分隔符。
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:
剪贴板的纯文本版本使用 CR 行结尾。这会将行尾转换为 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:
Unicode text
已等效采用text
和string
自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.
Unicode 文本和非 Unicode 文本之间不再有区别。只有一个文本类,名为“text”:即“foo”类返回文本。
回答by Zitoun
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"