I'm looking to make a function to split a camelCase string into separate words based on which letters are capitalized. So "camelCase" would become "Camel Case". I'm sure I could figure it out myself but I am probably too tired right now to concentrate because I'm not completely sure without creating a lot of code for parsing.
I guess I'd go through the string and take note of the location of each capital letter, then split it into separate words based on those letters locations, then add spaces between them and concat them all back together. Maybe someone has already created a function to do this?
Function to turn camelCase into separate words?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Function to turn camelCase into separate words?
How about..
Code: Select all
function splitCamel(s)
local function split(char)
return " " .. char
end
return (s:gsub("[A-Z]", split):gsub("^.", string.upper))
end
- Jasoco
- Inner party member
- Posts: 3727
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Function to turn camelCase into separate words?
That works! Wow, I really do not know how to use gsub. I would have probably used so much more code than needed.
Thanks!
Thanks!
Re: Function to turn camelCase into separate words?
I'm not trying to be rude, but you definitely don't want to redefine a static helper function each time you need it. Your example would be more appropriate as..bartbes wrote:How about..Code: Select all
-snip-
Code: Select all
do
local function split(char)
return " " .. char
end
function splitCamel(s)
return (s:gsub("[A-Z]", split):gsub("^.", string.upper))
end
end
Code: Select all
function splitCamel(s)
return (s:gsub("%u", " %1"):gsub("^.", string.upper))
end
Re: Function to turn camelCase into separate words?
Might also make sense to use frontier pattern depending on desired behavior for repeated caps.Onenmaru wrote:The problem can really just be solved with gsub's nth match syntax though.
Code: Select all
> print(("myFriedKFCChicken"):gsub("%u", " %1"):gsub("^.", string.upper))
My Fried K F C Chicken 1
Code: Select all
> print(("myFriedKFCChicken"):gsub(".%f[%l]", " %1"):gsub("%l%f[%u]", "%1 "):gsub("^.", string.upper))
My Fried KFC Chicken 1
Re: Function to turn camelCase into separate words?
For reference, this is how it is done in Rails' ActiveSupport (in Ruby):
https://github.com/rails/rails/blob/861 ... ods.rb#L90
Code: Select all
def underscore(camel_cased_word)
return camel_cased_word unless camel_cased_word =~ /[A-Z-]|::/
word = camel_cased_word.to_s.gsub(/::/, '/')
word.gsub!(/(?:(?<=([A-Za-z\d]))|\b)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1 && '_'}#{$2.downcase}" }
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
word.tr!("-", "_")
word.downcase!
word
end
Re: Function to turn camelCase into separate words?
Won't do much good as far as Lua's pattern matching is concerned (no alternation, lookbehinds of other fancy stuff). That's why I suggested frontier pattern, does the same job as the lookbehind in that example.S0lll0s wrote:For reference, this is how it is done in Rails' ActiveSupport (in Ruby)
Re: Function to turn camelCase into separate words?
I didn't mean that you should try to port it to lua, it was meant more as an algorithmic reference.airstruck wrote:Won't do much good as far as Lua's pattern matching is concerned (no alternation, lookbehinds of other fancy stuff). That's why I suggested frontier pattern, does the same job as the lookbehind in that example.S0lll0s wrote:For reference, this is how it is done in Rails' ActiveSupport (in Ruby)
Who is online
Users browsing this forum: Ahrefs [Bot], Amazon [Bot] and 10 guests