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.
Formatting (e.g. title case, lower case, etc.)
- Alexander Divine
- Posts: 65
- Joined: Mon Dec 21, 2009 7:01 pm
Re: Formatting (e.g. title case, lower case, etc.)
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
as for all upper and all lowercase, I would suggest string.upper() and string.lower() as perhaps the best answer
Re: Formatting (e.g. title case, lower case, etc.)
also, for title case, I found the following function online, which may be useful:
this was found at http://lua-users.org/wiki/StringRecipes
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
- Alexander Divine
- Posts: 65
- Joined: Mon Dec 21, 2009 7:01 pm
Re: Formatting (e.g. title case, lower case, etc.)
You guys are fantastic. This'll save me a ton of time.
- Alexander Divine
- Posts: 65
- Joined: Mon Dec 21, 2009 7:01 pm
Re: Formatting (e.g. title case, lower case, etc.)
For anyone interested, some ultra-easy functions to make text formatting simple.
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?
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
Re: Formatting (e.g. title case, lower case, etc.)
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
http://forums.mudlet.org/viewtopic.php? ... t=30#p2210
- Alexander Divine
- Posts: 65
- Joined: Mon Dec 21, 2009 7:01 pm
Re: Formatting (e.g. title case, lower case, etc.)
I like shortcuts, what can I say.
Re: Formatting (e.g. title case, lower case, etc.)
Well you made it be for example echo(upper(variable)) when echo(variable:upper()) did the same thing without having that