2014-10-27 30 views
1

我有一個簡單的字符串,其中列出了用逗號分隔的項目。在最後一項之前,我想包括「和」兩個字。使用AppleScript將逗號分隔的字符串添加到Word中「和」

例逗號分隔的字符串:

green apple, red strawberry, orange grapefruit, yellow banana 

我想代替顯示此:

green apple, red strawberry, orange grapefruit, and yellow banana 

此外,如果僅存在兩個項我想刪除逗號(即青蘋果和紅草莓)。

關於如何開始使用這個的任何想法?

謝謝!

+0

嗨。我回答了你的另一個問題。在某些時候,你必須瞭解人們向你展示什麼,並將這種學習應用於其他情況。我向你展示瞭如何在重複循環中使用if語句來控制你如何處理數據。一個簡單的if語句也適用於這裏。如果你在最後一個項目,然後添加「和」,當你把你的字符串在一起。就那麼簡單。你需要開始應用我們正在教你的東西,而不是繼續向人們詢問答案。你已經有了自己解決這個問題的工具。 – regulus6633 2014-10-27 15:12:17

回答

0

一種方法是使用文本項分隔符,統計數字,然後處理每種情況。這不會假設無關的逗號。

set s to "green apple, red strawberry, orange grapefruit, yellow banana" 
set otid to AppleScript's text item delimiters 
set AppleScript's text item delimiters to "," 
set s to text items of s 
set n to count of s 
if n = 1 then 
    set r to s as string 
else if n = 2 then 
    set r to item 1 of s & " and" & item 2 of s 
else 
    set item -1 of s to (" and" & item -1 of s) 
    set r to s as string 
end if 
set AppleScript's text item delimiters to otid 
return r