Scite Title Case

lua-users home
wiki

This simple function normalizes the case of a string to titlecase (e.g. from "MY string" to "My String").

function titlecase(str)
    --os.setlocale('pt_BR','ctype') -- set a locale as needed
    buf ={}
    local sel = editor:GetSelText()    
    for word in string.gfind(sel, "%S+") do          
        local first = string.sub(word,1,1)        
        table.insert(buf,string.upper(first) .. 
            string.lower(string.sub(word,2)))
    end    
    editor:ReplaceSel(table.concat(buf," "))
end
        

WalterCruz


That function didn't work for me (maybe I'm just to dumb to understant it). Anyways I made my own.

function titlecase(str)
result=''
    for word in string.gfind(str, "%S+") do          
        local first = string.sub(word,1,1)
        result = (result .. string.upper(first) ..
            string.lower(string.sub(word,2)) .. ' ')
    end    
    return result
end
        


That's because the first function is for SciTE. I've refactored Walter's original implementation into two functions, one which should work on standard Lua, and another which performs the SciTE operation.

Note that using table.concat() in this case is more efficient than appending to a string; see [Programming in Lua 11.6 String Buffers].

-- normalize case of words in 'str' to Title Case
function titlecase(str)
    local buf = {}
    for word in string.gfind(str, "%S+") do          
        local first, rest = string.sub(word, 1, 1), string.sub(word, 2)
        table.insert(buf, string.upper(first) .. string.lower(rest))
    end    
    return table.concat(buf, " ")
end

-- For use in SciTE
function scite_titlecase()
    local sel = editor:GetSelText()    
    editor:ReplaceSel(titlecase(sel))
end

MarkEdgar


Haven't tried this in a while, but was testing it out when I switched to SciTE version 1.74. With the last code sample using two functions, I was getting the error: SciTE_titlecase.lua:4: bad argument #1 to 'gfind' (string expected, got nil) I fixed the error, by switching the name of the passed string from str to sel in the line
function titlecase(str)
so that it becomes
function titlecase(sel)
Seems to work better now.


For me, this function does not work properly over multiple lines. Here's an alternative lifted from the String Recipes page:
local function tchelper(first, rest)
  return first:upper()..rest:lower()
end

function scite_titlecase()
    local sel = editor:GetSelText()
    sel = sel:gsub("(%a)([%w_']*)", tchelper)
    editor:ReplaceSel((sel))
end

Insert the above in your extensions file, and put the following into SciTEUser.properties:

command.name.6.*=Title Case
command.6.*=scite_titlecase
command.subsystem.6.*=3
command.mode.6.*=savebefore:no
command.shortcut.6.*=Ctrl+Alt+Z
Anonymous Steve.
I need to retain the original space characters between/before/after words, so I wrote this version:

function titlecase(str)
  local buf = {}
  local inWord = false
  for i = 1, #str do
    local c = string.sub(str, i, i)
    if inWord then
        table.insert(buf, string.lower(c))
      if string.find(c, '%s') then
        inWord = false
      end
    else
      table.insert(buf, string.upper(c))
      inWord = true
    end
  end
  return table.concat(buf)
end

I tested it with Lua 5.2.

I'm new to Lua, so forgive (or, better yet, correct) any style mistakes.

EllenSpertus?


RecentChanges · preferences
edit · history
Last edited October 5, 2013 1:36 am GMT (diff)