Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction  
1.1.
Camel case string
1.2.
Snake case string
2.
Approach to convert Snake case string to camel case string
3.
Code in HTML and Javascript
4.
Frequently Asked Questions
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Convert Snake Case String to Camel Case String

Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction  

In this blog, we will learn how to convert snake case string to camel case string. We will use javascript functions to convert snake case to camel case string. Before we learn the approach to this conversion, let's first learn what camel and snake case strings are.

Camel case string

A Camel case string is a string in which the middle word of the string is uppercase. The rest of the letter of the word needs to be lower case. The first letter of the first word is not capital. There are no spaces and hyphens in the rest of the string.

For example:

string-” hello cat”

Pascal string-”helloCat”

 

string-” good morning”

Pascal string-”goodMorning

Snake case string

A snake case string is a string in which all the letters are lowercase. The words are delimited or separated by an underscore. There are no spaces between words in the snake case string

For example:

string-” hello world”

Pascal string-”hello_world”

 

string-” good morning”

Pascal string-”good_morning”

Approach to convert Snake case string to camel case string

We will be given a snake  case string, and we will a javascript function to convert it into a camel case string.

We have used replace(), toUpperCase() functions to convert snake case string to camel case string. First, we will replace all the underscores with nothing; then, we will convert the first letter of each word to a capital letter except for the first word.

Before we write the code, lets have a look at an example:

Input : “hello_dog”

Output:”helloDog”

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Code in HTML and Javascript

<!DOCTYPE html>
<html>
   <head>
      <title>
         How to convert a snake case string to
         camel case using JavaScript?
      </title>
   </head>
   <body style="text-align: center;">
      <h1 style="color: blue;">
         CODING NINJAS
      </h1>
      <h3>
         How to convert a snake case string to
         camel case using JavaScript?
      </h3>
      <script>
         function snakeToCamel(str){
             return str.replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase());
         }
         s1='string_case_hello'
         s2='camel_case'
         console.log(snakeToCamel(s1)); 
         console.log(snakeToCamel(s2));   
             
      </script>
   </body>
</html>

 

Output:

Frequently Asked Questions

  1. What is the use of javascript language?
    It is a language that deals with both the client side and server-side, and also helps to make the website more interactive.
     
  2. What is the use of a console in javascript?
    In javascript, a console is an object used to gain access to the browser debugging console.
     
  3. What is the use of the string replace() function in javascript?
    It is a method that searches the string for a particular value or expression and the change it with what the user wants.

Conclusion

In this blog, we discussed what a snake and camel case string is. We also learned to convert a snake case string to a camel case string. We have used javascript language to run this program. 

Check out this article - String slicing in Python

We hope that this blog has helped you enhance your program knowledge to convert snake case string to camel case string and if you would like to learn more, check out our articles on Javascript. Do upvote our blog to help other ninjas grow. 

Happy Coding! 

Previous article
Convert Pascal Case String to Snake Case String
Next article
Differentiate between Array Slice and Splice Javascript
Live masterclass