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

Convert Pascal Case String to Snake 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 to convert the pascal case string to the snake case string. We will use javascript language to do this program. Before learning to convert pascal to snake case string, let's learn what pascal case string and snake case string are.

Pascal case string

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

For example:

string-” hello javascript.”

Pascal string-”HelloJavascript”

 

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 javascript.”

Pascal string-”hello_javascript.”

 

string-” good morning”

Pascal string-”good_morning”

Approach to convert Pascal case string to snake case string

We will be given a pascal string, and using the javascript function, we will convert it into a snake case string.

We have used split(), join(), toLowerCase() functions to convert pascal case string to snake case string. First, we will split the given string into arrays using the split() function; then, we will join these arrays into a string with an underscore as a separator. After that, we will convert the string to lowercase using the toLowerCase() function.

Let’s see some examples:

Input: “HaveGoodDay”

Output: “have_good_day”

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 pascal case string to
         snake case using JavaScript?
      </title>
   </head>
   <body style="text-align: center;">
      <h1 style="color: blue;">
         CODING NINJAS
      </h1>
      <h3>
         How to convert a pascal case string to
         snake case using JavaScript?
      </h3>
      <script>
         var string1 = 'GoodMorningIndia';
         var string2 = 'JavascriptCode';
         
         var out1 = string1.split(/(?=[A-Z])/).join('_').toLowerCase();
         var out2 = string2.split(/(?=[A-Z])/).join('_').toLowerCase();
         
         console.log(out1);
         console.log(out2);
         
      </script>
   </body>
</html>

 

Output:

Frequently Asked Questions

  1. What is a console in javascript?
    In javascript, a console is an object used to gain access to the browser debugging console.
     
  2. What is camel case string?
    The Camel case string is almost exactly like a pascal case string, but the first letter of the first word is not capitalized in the camel case string.
     
  3. What are the parameters of the split function in javascript?
    Parameters of split function are separator and limit. A separator is a regular expression used for the separation of words in the string. A limit is an integer that limits the number of separations.

Conclusion

In this blog, we discussed what a pascal case string is and what is a snake case string. We also learned to convert a pascal string to a snake case string using Javascript.

Check out this problem - Multiply Strings

We hope that this blog has helped you enhance your knowledge regarding program to convert pascal case string to snake 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
ES5 vs ES6
Next article
Convert Snake Case String to Camel Case String
Live masterclass