Formatting (e.g. title case, lower case, etc.)

Post Reply
User avatar
Alexander Divine
Posts: 65
Joined: Mon Dec 21, 2009 7:01 pm

Formatting (e.g. title case, lower case, etc.)

Post by Alexander Divine »

I migrated from zMud and so I'm a little spoiled.

What I want is a way to convert a string to proper/title case, or all uppercase, or any number of things.

I'm pretty much writing a function to do this right now, but is there an existing one? If so, I'm gonna slap myself.

User avatar
demonnic
Posts: 886
Joined: Sat Dec 05, 2009 3:19 pm

Re: Formatting (e.g. title case, lower case, etc.)

Post by demonnic »

not sure what you mean by titlecase, unless you mean First Letter Of Every Word Capitalized


as for all upper and all lowercase, I would suggest string.upper() and string.lower() as perhaps the best answer

User avatar
demonnic
Posts: 886
Joined: Sat Dec 05, 2009 3:19 pm

Re: Formatting (e.g. title case, lower case, etc.)

Post by demonnic »

also, for title case, I found the following function online, which may be useful:

Code: Select all

function titlecase(str)
    local buf = {}
    for word in string.gfind(sel, "%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

this was found at http://lua-users.org/wiki/StringRecipes

User avatar
Alexander Divine
Posts: 65
Joined: Mon Dec 21, 2009 7:01 pm

Re: Formatting (e.g. title case, lower case, etc.)

Post by Alexander Divine »

You guys are fantastic. This'll save me a ton of time.

User avatar
Alexander Divine
Posts: 65
Joined: Mon Dec 21, 2009 7:01 pm

Re: Formatting (e.g. title case, lower case, etc.)

Post by Alexander Divine »

For anyone interested, some ultra-easy functions to make text formatting simple.

Code: Select all

function title(str)
  str = str:gsub("^%l", string.upper, 1)
  return str
end

function upper(str)
  str = string.upper(str)
  return str
end

function lower(str)
  str = string.lower(str)
  return str
end
Actually, here's a miscellaneous question: for the title-case string above, I added a return and it pretty much worked without incident: can anyone break down exactly what str:gsub means?

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: Formatting (e.g. title case, lower case, etc.)

Post by Vadi »

I don't quite get the point of last two functions, but for capitalization, we'll have this added soon:

http://forums.mudlet.org/viewtopic.php? ... t=30#p2210

User avatar
Alexander Divine
Posts: 65
Joined: Mon Dec 21, 2009 7:01 pm

Re: Formatting (e.g. title case, lower case, etc.)

Post by Alexander Divine »

I like shortcuts, what can I say.

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: Formatting (e.g. title case, lower case, etc.)

Post by Vadi »

Well you made it be for example echo(upper(variable)) when echo(variable:upper()) did the same thing without having that :)

Post Reply