Conversion from string to different ways of word combination in JavaScript. Show In programming, there are several practices of writing compound words or phrases. These practices are used as conventions in almost every programming language and not fix from one to another. The choice of naming conventions always be a controversial issue, with some are holding theirs to be the best and others to be inferior. Moving forward, before we can do some string conversion, we need to recognize the case styles first. We will learn 4 different case styles which are:
1. Snake casePhoto by David Clode on UnsplashSnake case is the practice of writing compound words in which the words are separated by one underscore character and no spaces. The first letter is either upper- or lowercase. It is commonly used in declaring variable names, function names, and sometimes computer’s filenames. Example Hello_world 2. Kebab caseIn Kebab case, all letters are written in lower case and the words are separated by a hyphen or minus sign. The kebab notation is often used as a convention for naming filenames. Example kebab-case
3. Camel caseCamel case describes the practice of writing phrases such that each word or abbreviation in the middle of the phrase begins with a capital letter, without intervening spaces or punctuation. Example camelCase 4. Pascal casePascal case is similar to camel case but the first letter is in uppercase. There is no space and hyphen to separate the words. The pascal case is often used as a convention in creating class in many programming language. Example MainBuilding Let’s start with the string conversion as we already recognized every case styles above. String ConversionIn Javascript, there are various ways you can convert a string to the 4 case styles mentioned above. I will share the method that I used which is by using RegEx (regular expression). You can start by
creating a new js file called node main.js 1. String to Snake Case Convert string to snake caseRegEx /[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g Result 2. String to Kebab Case Convert string to kebab caseI use the same RegEx as in conversion to snake case above. Result Result of conversion to kebab case3. String to Camel Case RegEx /[^a-zA-Z0-9]+(.)/g Result Result of conversion to camel case4. String to Pascal Case Convert string to pascal caseRegEx /\w\S*/g Result Result of conversion to pascal caseConclusionThroughout this article, we have learned on how to distinguish 4 case styles which are often seen in any programming language as a convention. We also got the idea on how to convert a string to these 4 case styles. If you think this article was helpful, don’t forget to share it with your friends. How do you make a sentence case in JavaScript?Title Case a sentence in javascript. Divide all the words in the sentence individually. ... . Convert all the elements in each and every word in to lowercase using string. ... . Loop through first elements of all the words using for loop and convert them in to uppercase. ... . Join all the words using String.. Is JavaScript snake case?Snake case (also referred to as underscore case) is when all the letters of the word are lower case but delimited by an underscore. We seldom use the snake case coding convention in C-style languages like Java, JavaScript, and TypeScript.
What is toLocaleUpperCase?The toLocaleUpperCase() method converts a string to uppercase letters, using current locale. The locale is based on the language settings of the browser. The toLocaleUpperCase() method does not change the original string.
How do you convert a string to a proper case?ToTitleCase() method that we can use to convert strings to title case:. public string ToTitleCase (string str);. var textinfo = new CultureInfo("en-US", false). TextInfo;. var textInfo = CultureInfo. CurrentCulture. TextInfo;. Console. WriteLine(textinfo. ToTitleCase("OLIVER TWIST". ToLower()));. |