Selected words to uppercase javascript
Okay I have a small problem I want to make specific words in this string to be upperCase() or lowerCase(). Show
This is what I originally tried:
All I want to know is how to simply make certain words
Filipe 1,7382 gold badges13 silver badges18 bronze badges asked Jan 19, 2016 at 15:13
5 you can do this way,for any particular word. here is an example using toUpperCase().
or this way, here is an example using substring() and toLowerCase().
answered Jan 19, 2016 at 16:00
1 From a question in StackOverflow:
You can write "Hello World" or "HellO woRld" and the code works in both cases.
answered Jan 19, 2016 at 15:32
In this article, you are going to learn how to capitalize the first letter of any word in JavaScript. After that, you are going to capitalize the first letter of all words from a sentence. The beautiful thing
about programming is that there is no one universal solution to solve a problem. Therefore, in this article you are going to see multiple ways of solving the same problem. First of all, let's start with capitalizing the first letter of a single word. After you learn how to do this, we'll proceed to the next level – doing it on every word from a sentence. Here is an example: In JavaScript, we start counting from 0. For instance, if we have an array, the first position is 0, not
1. Also, we can access each letter from a String in the same way that we access an element from an array. For instance, the first letter from the word "freeCodeCamp" is at position 0. This means that we can get the letter f from freeCodeCamp by doing In the same way, you can access other letters from the word. You can replace "0" with any number, as long as you do not exceed the word length. By exceeding the word length, I mean
trying to do something like Now that we know how to access a letter from a word, let's capitalize it. In JavaScript, we have a method called For instance: Running the above code, you are going to get a capital F instead of f. To get the whole word back, we can do this: Now it concatenates "F" with "reeCodeCamp", which means we get back the word "FreeCodeCamp". That is all! To be sure things are clear, let's recap what we've learnt so far: The next step is to take a sentence and capitalize every word from that
sentence. Let's take the following sentence: We have to capitalize the first letter from each word from the sentence The first step we take is to split the sentence into an array of words. Why? So we can manipulate each word individually. We can do that as follows:
Iterate over each wordAfter we run the above code, the variable
Now the next step is to loop over the array of words and capitalize the first letter of each word. In the above code, every word is taken separately. Then it capitalizes the first letter, and in the end, it concatenates the capitalized first letter with the rest of the string. Join the wordsWhat is the above code doing? It iterates over each word, and it replaces it with the uppercase of the first letter + the rest of the string. If we take "freeCodeCamp" as an example, it looks like this After it iterates over all the words, the The last step is to join all the words to form a sentence. So, how do we do that? In JavaScript, we have a method called
In the above code snippet, we can see the join method in action. We call it on the Therefore, Other methodsIn programming, usually, there are multiple ways of solving the same problem. So let's see another approach.
What is the
difference between the above solution and the initial solution? The two solutions are very similar, the difference being that in the second solution we are using the Let's go even further, and try to do a one-liner. Be aware! One line solutions might look cool, but in the real world they are rarely used because it is challenging to understand them. Code readability always comes first.
The above code uses RegEx to transform the letters. The RegEx might look confusing, so let me explain what happens:
Thus, with one line, we accomplished the same thing we accomplished in the above solutions. If you want to play around with the RegEx and to learn more, you can use this website. ConclusionCongratulations, you learnt a new thing today! To recap, in this article, you learnt how to:
Thanks for reading! If you want to keep in touch, let's connect on Twitter @catalinmpit. I also publish articles regularly on my blog catalins.tech if you want to read more content from me. Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started How do you capitalize certain letters in JavaScript?To capitalize the first character of a string, We can use the charAt() to separate the first character and then use the toUpperCase() function to capitalize it.
Is there a capitalize function in JavaScript?In JavaScript, we have a method called toUpperCase() , which we can call on strings, or words. As we can imply from the name, you call it on a string/word, and it is going to return the same thing but as an uppercase. For instance: const publication = "freeCodeCamp"; publication[0].
How do you capitalize every word?To use a keyboard shortcut to change between lowercase, UPPERCASE, and Capitalize Each Word, select the text and press SHIFT + F3 until the case you want is applied.
How can I convert each alternate character of a string to uppercase in JavaScript?To convert a character to upper case you can do Character. toUpperCase(ch) ; I suggest you build a StringBuilder from these characters which you can toString() when you are done.
|