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”