2,875
edits
No edit summary |
No edit summary |
||
Line 107: | Line 107: | ||
return string.upper(head) .. string.lower(tail) | return string.upper(head) .. string.lower(tail) | ||
end | end | ||
end | |||
-- Converts an input string into TitleCase. | |||
-- Every first letter of every word becomes uppercase | |||
-- With the exception of 'of', 'the', 'and', but only if these | |||
-- appear anywhere in the sentence besides the first occurence. | |||
-- Examples: | |||
-- specialTitleCase('ALL CAPS') = 'All Caps' | |||
-- specialTitleCase('all lowercase') = 'All Lowercase' | |||
-- specialTitleCase('A MiXTUre') = 'A Mixture' | |||
-- specialTitleCase('the bones') = 'The Bones | |||
-- specialTitleCase('of the world') = 'Of the World' | |||
-- specialTitleCase('amulet Of Fishing') = 'Amulet of Fishing' | |||
function p.specialTitleCase(sentence) | |||
-- List of words that are excluded from TitleCase | |||
local excludedWords = { | |||
["of"] = true, | |||
["and"] = true, | |||
["the"] = true | |||
} | |||
-- Split all words and add them as lower case to the table. | |||
local words = {} | |||
for word in sentence:gmatch("%S+") do | |||
table.insert(words, word:lower()) | |||
end | |||
-- Capitalize the first word | |||
words[1] = words[1]:gsub("^%l", string.upper) | |||
-- Title-case the remaining words, excluding certain words based on position | |||
for i = 2, #words do | |||
local curWord = words[i] | |||
if excludedWords[curWord] == true then | |||
else | |||
words[i] = curWord:gsub("^%l", string.upper) | |||
end | |||
end | |||
return table.concat(words, " ") | |||
end | end | ||
edits