DEV Community

Farhad
Farhad

Posted on • Edited on

Convert string to camel case CodeWars Kata

Codewars

Codewars is a platform that helps you learn, train, and improve your coding skills by solving programming tasks of many types and difficulty levels. Here is the first Kata I've solved here, and I would like to share my solutions here in DEV.

What is a Kata?

On Codewars, kata is code challenges focused on improving skill and technique.

❓ Here is my first Kata:

Link to the Kata

Write a method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output should be capitalized only if the original word was capitalized (known as Upper Camel Case, also often referred to as Pascal case).

Examples

"the-stealth-warrior" gets converted to "theStealthWarrior"
"The_Stealth_Warrior" gets converted to "TheStealthWarrior"

πŸ’‘ We can use Regex to perform a search and replace in almost all programming languages.

A regular expression is a sequence of characters that specifies a match pattern in text. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation. Regular expression techniques are developed in theoretical computer science and formal language theory.

I used preg_replace_callback function in PHP which performs a regular expression search and replace using a callback to find "_\w" or "-\w" patters like _a or -b and replace them with the equivalent uppercase character. like _b => B so the-stealth will be theStealth.

πŸ’» So, my solution in PHP is:

function toCamelCase($str){
    return preg_replace_callback('/(\-|\_)([a-z])/i', function ($match) {
        return strtoupper($match[2]);
    }, $str);
}
Enter fullscreen mode Exit fullscreen mode

You might ask what is that $match[2], well here is the var_dump($match) output:

array(3) {
  [0]=>
  string(2) "_s"
  [1]=>
  string(1) "_" 
  [2]=>
  string(1) "s" 
}
Enter fullscreen mode Exit fullscreen mode

as you see inside of each iteration this array contains, the matched string beside regex groups! in our case here we can access that group by $match[2]`.

Thank You ❀️

I wish my posts to be useful to anyone who's new to the world of programming or anybody who's curious 🧐!
If you find the content useful to you, please comment your thoughts, I would love to learn from you all.

Thank you for your loving directions.

Top comments (2)

Collapse
 
kaungpyaeminthein profile image
Kaung Pyae Min Thein

I understand the Idea of the solution until here-> "B so the-stealth will be theStealth." . But I don't understand the code and follow info . No offense. May be I don't understand PHP well or I am stupid.......

Collapse
 
feriun profile image
Farhad

Hey there,

Yes you right, this part of my write-up is a bit 😡. This is actually an example I tried to show you, this is not a part of the solution, so if I want to recap:

πŸ”Ž First I find the pattern with this regex /(\-|\_)([a-z])/i.

It will find all _ or - followed by a character, like _c or _E or -a.

✏ Second I replace all these found matches with the equivalent uppercase character; So if we have _b this will be replaced by B! This is what I meant by _b => B.

I do the replacement with the preg_replace_callback function in PHP; Inside of each iteration the $match array contains, the matched string beside regex groups!

βœ” We need to uppercase the character after _ or - so I use the second group of my regex, we can have access to that group in the second index of our array so I return strtoupper($match[2]) inside the callback function.