This topic contains 2 replies, has 1 voice, and was last updated by  Jeff L 10 years, 1 month ago.

Capitalize functionality

  • Lynn asked in our old forum “The Capitalize function just capitalizes the first letter of the first word in a string. Because some names and cities can be two words, I’m looking for a function that capitalizes all the words in a string, so ‘Los Angeles’ rather than ‘Los angeles’ or ‘Peggy Sue’ rather than ‘Peggy sue’. Has anyone done that on their interface?”

    I was messing around with this yesterday and used the gmatch to run through a string and capitalize. However, i also added it so that it could handle ‘Mc’s and Mac’s as well. try creating a shared module and use this.
    –Begin capitalize function —————————————————
    –Table that can be altered for names that need unique capitalization
    local C = {

    function string.allCaps(A)
    local final = ”
    for w in A:gmatch(‘%w+’) do
    –check if it is a mc or mac etc
    B, altered = ck(w)
    –if not a mc or mac then standard caps
    if altered ~= true then
    B = normalCaps(w)
    –build full string to return
    final = final..’ ‘..B
    return final

    function normalCaps(A)
    –if it does not match any in the tables return capital
    return A:sub(1,1):upper()..A:sub(2):lower()

    function ck(A)
    –if it matches a name in C table then adjust
    for k,v in pairs© do
    if A:lower():sub(1, v:len()) == v then
    A = A:sub(1,1):upper()..
    A:sub(v:len()+1,v:len()+1 ):upper()..
    return A, true
    return _, false
    –END capitalize function —————————————————

    Then you can just call it from your script and send it a string like so;
    local String = “mcdonalds 7702 macknight Road, Pittsburgh, PA”
    local Final = String:allCaps()

    it may still need a little tweaking but it is a good start I think

    Garry Christensen: G’day Lynn,
    I like a challenge so here’s what I came up with (although I did steal Jeffery’s idea about Mc and Mac but added a little more)

    print (string.proper(“mr charles harrington-o’tool, day/night manager, mcdonalds restaurant, 7702 macknight road, pittsburgh, PA”, true ))

    –> Mr Charles Harrington-O’Tool, Day/Night Manager, McDonalds Restaurant, 7702 MacKnight Road, Pittsburgh, PA

    function string:proper(bPreserveUpper)
    local tPattern = {‘ Ma?c%a’, ‘%a+%-%a’, “[ %-]O’%a”, ‘%a+/%a’}

    — add a space to beginning for reasons that will become obvious
    local sResult = ‘ ‘ .. self

    if not bPreserveUpper then
    — start with everything in lower case
    sResult = sResult:lower()

    — capitalise the first letter following a space
    sResult = sResult:gsub(‘ %a’, function (s)
    return s:upper()

    — now loop through the patterns and capitalise the last letter in the pattern
    for nCntr = 1, #tPattern do
    sResult = sResult:gsub(tPattern[nCntr], function (s)
    return s:sub(1, -2) .. s:sub(-1):upper()

    — return all except the added first space
    return sResult:sub(2)

You must be logged in to reply to this topic.