我在标题案例中有一个字符串,但文章应为小写。我试图将文章改为小写。这就是我现在所拥有的:
set theStringTitleCase to "Iranian General Killed In Syria"
set theListTitleCase to every word of theStringTitleCase
set articles_in_title to {"in", "to", "at"}
set fixed_words_in_title to {}
repeat with the_word in theListTitleCase
repeat with the_article in articles_in_title
if the_word contains the_article then
set the end of fixed_words_in_title to the_article
else
set the end of fixed_words_in_title to the_word
end if
end repeat
end repeat
set theStringTitleCase to fixed_words_in_title as string
return theStringTitleCase
不确定我哪里出错了
答案 0 :(得分:0)
这应该可以解决问题。
set theStringTitleCase to "Iranian General Killed In Syria"
set lowerCasedArticles to small_articles(theStringTitleCase)
log lowerCasedArticles
--> (*Iranian General Killed in Syria*)
to small_articles(aString)
set capArticles to {" In ", " To ", " At "} # spaces are mandatory!
set smallArticles to {" in ", " to ", " at "}
# fail early conditions . . .
if (count capArticles) ≠ (count smallArticles) then error "unequal number of elements in lists "
if aString is "" then return ""
# we replace capitalized articles with lower cased ones below.
tell (a reference to my text item delimiters)
set tids to contents of it
repeat with i from 1 to (count capArticles)
set contents of it to item i of capArticles
set oldlist to text items of aString
set contents of it to item i of smallArticles
set aString to oldlist as string
end repeat
set contents of it to tids
return aString
end tell
end small_articles