Change Case


These functions can be used to change a string to either Sentence Case (first word capitalized) or Title Case. (each word capitalized)

function SentenceCase(input)
assert(type(input) == 'string', ('SentenceCase must receive a string. Received %s instead.'):format(type(input)))
return (input:gsub('[^.!?]+', function(sentence)
local space, first, rest = sentence:match('(%s*)(.)(.*)')
return space .. first:upper() .. rest:lower():gsub("%si([%s'])", ' I%1')
end))
end
function TitleCase(input)
assert(type(input) == 'string', ('TitleCase must receive a string. Received %s instead.'):format(type(input)))
return (input:gsub('(%S)(%S*)', function(first, rest) return first:upper() .. rest:lower() end))
end