Hello! It’s been a long time since we met isn’t it. Sorry for being lazy. I hope I manage to revive this series, and actual continue it. I am still looking for people to help me out and make challenges as well! So if you’re interested, help me out!
As usual, same requirements:
Has to be written in Lua 5.1 (the version roblox uses)
The program has to be compatible with Roblox Studio, meaning you can’t use features vanilla Lua has but rbxlua disabled
You can’t use httpservice to cheat the challenge
Your company has a new CEO, and this new CEO hates the snake_case naming convention so much! He wants you to convert all snake_case variables names to camelCase.
Write a function camelCase(s), where s is a string containing a snake_case name, that returns the camelCase version of snake_case.
local function camelCase(s)
local words = {}
for i, v in pairs(string.split(s,"_")) do --grab all the words separated with a _ underscore
if i > 1 then --we don't wanna capitalize the first letter
table.insert(words, v:sub(1,1):upper()..v:sub(2)) --we take the first character, uppercase, and add the rest. Then I insert to the table
end
end
return table.concat(words,"") --just concat everything inside of the table
end
Made a version using gsub that is pretty fast as well as staying readable:
local function camelCase(s)
return string.gsub(s, "_%a+", function(word)
local first = string.sub(word, 2, 2)
local rest = string.sub(word, 3)
return string.upper(first) .. rest
end)
end
local function _tocamel(s)
return string.upper(string.sub(s,1,1))..string.sub(s,2)
end
local function tocamel(s)
return (string.gsub(s,"_(%w+)",_tocamel))
end
print(tocamel"this_should_work") --> thisShouldWork
local function camelCase(s)
local sep = string.split(s,"_")
local strs = {}
for i, v in ipairs(sep) do
if i ~= 1 then
local firstletter = string.sub(v,1,1)
local upper = string.gsub(v,"^%l",string.upper(firstletter))
table.insert(strs, upper)
else
table.insert(strs, v)
end
end
print(table.concat(strs,""))
end
camelCase("snake_case")
camelCase("this_is_an_example")
camelCase("var")
decided to make the worst solution i could think of while still keeping it relatively readable. this is the result. (also no gsub because gsub is too easy)
solution
local function toCamelCase(inp)
local t = setmetatable(inp:split("_"),{__index = table})
t:foreach(function(k,v)
if k > 1 then
t[k] = v:sub(1,1):upper()..v:sub(2,#v)
end
end)
return t:concat()
end
print(toCamelCase("hello_there_starmaq")) --> "helloThereStarmaq"
Not exactly the nicest looking code but it’s me first try
function camelCase(s)
local CurrentWord
for Num,Word in pairs(string.split(s,"_"))do
CurrentWord = Num == 1 and string.lower(Word) or (CurrentWord..string.gsub(Word,"^%l", string.upper))
end
return CurrentWord
end
camelCase("snake_case") --snakeCase
camelCase("this_is_an_example") --thisIsAnExample
camelCase("var") --var
Alright. Heres a solution without the string library’s functions gsub, split, or upper.
function simpleStyleConversion (str)
local new_string = ''
local upper_flag=0
local function upperCaseNext()
upper_flag=-32
return ''
end
for i=1, #str do
local byte = string.byte(str, i) + upper_flag
upper_flag = 0
new_string = new_string .. (byte~=95 and string.char(byte) or upperCaseNext())
end
return new_string
end
Split by every character, find and remove any underscores and uppercase the character after it.
local function camelCase(givenString)
local characterArray = string.split(givenString, ".");
while (true) do
local index = table.find(characterArray, "_");
if (index) then
characterArray[index + 1] = string.upper(characterArray[index + 1]);
table.remove(characterArray, index);
else
return table.concat(characterArray, "");
end
end
end
local s = "snake_case_eee_case_case_case"
local function camelCase(s)
local g = s:gsub("%_(%w+)", function(part) local before, after = part:sub(1,1), part:sub(2) return before:upper()..after end)
return g
end
print(camelCase(s))
local function camelCase(str)
local strings = {}
local snakeCaseString = str
for i, splitted in ipairs(snakeCaseString:split("_")) do
table.insert(strings, i, splitted)
end
local camelCaseConvert = ""
-- Upper and Combine
for i, str in ipairs(strings) do
if i ~= 1 then
str = str:gsub("^%l", string.upper)
end
camelCaseConvert = camelCaseConvert .. str
end
return camelCaseConvert
end
print(camelCase("i_am_gaming")) --> iAmGaming
Hi, i just made this, very simple to read and works perfectly
local function toCamel(text: string)
if not text:match("_%a") then
return
end
local toUpper = text:gsub("_%a", string.upper)
local Result = toUpper:gsub("_", "")
return Result
end
local Formated = toCamel("snake_case") --snakeCase
print(Formated)
print(toCamel("hello_there_how_are_yall_doing")) --helloThereHowAreYallDoing
One of my favourite challenges so far, here’s my take at it.
local function camelCase(str)
local splitString = str:split("_")
if (#splitString < 2) then return end -- dont do anything if the string had do underscores in it
for k, v in ipairs(splitString) do
-- Don't do anything at the first index (camelCase)
if (k > 1) then
splitString[k] = v:sub(1, 1):upper() .. v:sub(2, #v)
end
end
return table.concat(splitString, "")
end
print(camelCase("snake_snake_snake_snake"))
local function camelCase(input: string) : string
input = string.split(input, '_');
for index: number = 2, #input do
input[index] = string.upper(input[index]:sub(1, 1)) .. input[index]:sub(2, input[index]:len());
end
return table.concat(input);
end
local function camel_case(s) -- :P
local after = false
local new = ""
for letter in string.gmatch(s, ".") do
if letter == "_" then
after = true
elseif after then
new = new .. string.upper(letter)
after = false
else
new = new .. letter
end
end
return new
end
print(camel_case("hello_world_lol"))
Well, I’m late to this… anyways, I had my own take, and it’s similar to yours!
local function camelCase(input)
local split = string.split(input,"_")
local final = {}
for i,v in pairs(split) do
if i == 1 then
table.insert(final,v)
else
local findReplace = string.sub(v,1,1)
local finalString = string.gsub(v,findReplace,string.upper(findReplace))
print(finalString)
table.insert(final,finalString)
end
end
return table.concat(final,"")
end
print(camelCase("snake_yes"))
print(camelCase("snake_yes_person_thingy"))