Write a Program That Reads a Line of Input as a String Then Prints Only Uppercase Lettters

JavaScript toLowerCase() – How to Convert a String to Lowercase and Uppercase in JS

This commodity explains how to convert a string to lowercase and uppercase characters.

We'll also go over how to brand only the get-go letter in a give-and-take uppercase and how to make the start letter of every word in a sentence uppercase.

Let'due south get started!

How to use the toLowerCase() method in JavaScript

The toLowerCase method converts a string to lowercase letters.

The general syntax for the method looks similar this:

                String.toLowerCase()                              

The toLowerCase() method doesn't take in whatsoever parameters.

Strings in JavaScript are immutable. The toLowerCase() method converts the string specified into a new ane that consists of only lowercase letters and returns that value.

It ways that the old, original string is not inverse or affected in any way.

                permit myGreeting = 'Hey there!';  console.log(myGreeting.toLowerCase());  //output //hey there!                              

The string myGreeting consists of but one upper-case letter alphabetic character that gets converted to lowercase.

Whatsoever letters that are already lowercase are not affected by the toLowerCase() method, just uppercase ones. These messages preserve their original class.

The cord in the example beneath consists of all uppercase messages. They are all so converted to lowercase when the toLowerCase() method is practical.

                const  anotherGreeting = 'Skilful Morn!!';  console.log(anotherGreeting.toLowerCase()); //output //good morning!!                              

How to employ the toUpperCase() method in JavaScript

The toUpperCase() method is like to the toLowerCase() method but it instead converts the string value to uppercase.

The general syntax for calling the method looks like this:

                Cord.toUpper()                              

Information technology doesn't take in any parameters.

As strings in JavaScript are immutable, the toLowerCase() method does not modify the value of the string specified.

Information technology instead returns a new value. The string specified is converted to a new one whose contents consist of just all majuscule messages. This means that at that place volition now be ii strings: the original and the newly converted capitalized one.

                panel.log('I am shouting!'.toUpperCase());  //output //I AM SHOUTING!                              

Any capital letters already in the string volition not be affected and will remain unchanged when the toLowerCase() method gets chosen.

How to capitalize only the first alphabetic character in a cord in JavaScript

What if you want to make just the showtime alphabetic character of a cord a upper-case letter?

Below is a uncomplicated example that shows you one way to do just that.

Say there is a variable called myGreeting with the string value of hello, in all lowercase messages.

                permit myGreeting = 'hello';                              

You lot kickoff locate and excerpt the first letter of that string past using its index. Then you telephone call the toUpperCase() method on that specific letter.

Equally a reminder, indexing in JavaScript (and most programming languages) starts at 0, so the first letter has an index of 0.

Salve this operation in a new variable called capFirstLetter.

                let capFirstLetter = myGreeting[0].toUpperCase();  panel.log(capFirstLetter); // returns the alphabetic character 'H' in this case                              

Next, yous desire to isolate and cut off that first character and keep the residue of the string.

One way to do this is by using the piece() method. This creates a new string starting from the alphabetize specified until the end of the word.

You want to offset from the second letter until the end of the value.

In this case, the statement you should pass to slice() is an index of 1 since that is the index of the 2d letter.

This way, the first character is excluded altogether. A new string is returned without it but containing the rest of the characters – minus that first letter.

And so salve that operation to a new variable.

                let restOfGreeting = myGreeting.slice(1);  panel.log(restOfGreeting); //returns the cord 'ello'                              

Past combining the two new variables with concatenation, you lot go a new string with just the start letter capitalized.

                let newGreeting = capFirstLetter + restOfGreeting;  panel.log(newGreeting); //Howdy                              

Another mode is to combine the steps from above and isolate them in a function.

The function gets created just one time. The function and so returns a new string with the first letter capitalized.

The corporeality of code you need to write is substantially less while also being able to pass in any string as an argument without writing repetitive lawmaking.

                part capFirst(str) {      render str[0].toUpperCase() + str.slice(1);  }  console.log(capFirst('how-do-you-do')); //output  //Hullo                              

How to capitalize the first letter of the alphabet of every word in JavaScript

But how do y'all brand the first letter of every word in a judgement uppercase?

The method shown in the section higher up won't work as it doesn't deal with multiple words, just a single word in a sentence.

Say you accept a sentence like the 1 below. You want to capitalize every first give-and-take in the sentence.

                allow learnCoding = 'acquire to code for free with freeCodeCamp';                              

The commencement pace is to split up the sentence into individual words and work with each 1 separately.

For that, you utilize the separate() method and pass a space every bit an argument. It means that with every space in the sentence provided, an detail gets passed into a new assortment.

Information technology splits the judgement based on blank spaces.

Create a new variable and store the new array.

                allow splitLearnCoding = learnCoding.split up(" ");  panel.log(splitLearnCoding);  //['learn', 'to', 'code', 'for', 'gratis', 'with', 'freeCodeCamp']                              

Now from that sentence, there is a new array of words that allows yous to manipulate each word on its own, separately.

Since there is now a new array, you tin can use the map() method to iterate over each individual detail inside it.

In the map() method, you use the same process shown in the section above to have each word individually, capitalize the first letter, and return the rest of the word.

                let capSplitLearnCoding = splitLearnCoding.map(word => {     return give-and-take[0].toUpperCase() + word.slice(1); })  panel.log(capSplitLearnCoding); //['Learn', 'To', 'Code', 'For', 'Complimentary', 'With', 'FreeCodeCamp']                              

The first letter of the alphabet of every discussion is now capitalized.

All that is left now is to combine the words in the array together in a unmarried sentence again.

For that you utilise the join() method and pass a space equally the argument.

                allow learnCodingNew = capSplitLearnCoding.join(" ");  console.log(learnCodingNew); //Acquire To Code For Free With FreeCodeCamp                              

As shown in the section above, you lot can besides create a function that combines all theses steps. You volition then exist able to pass as an statement any string and each first give-and-take in information technology will be capital.

                function capFirstLetterInSentence(sentence) {     let words = judgement.dissever(" ").map(word => {         return word[0].toUpperCase() + give-and-take.slice(1);     })     render words.join(" "); }  panel.log(capFirstLetterInSentence("i am learning how to code")); //I Am Learning How To Code                              

Conclusion

And there you have it! This is how you apply the toLowerCase() and toUpperCase() methods in JavaScript.

You learned how to capitalize the first letter of a word and capitalize the first letter of each discussion in a judgement.

If y'all desire to learn JavaScript and gain a better understanding of the linguistic communication, freeCodeCamp has a costless JavaScript Certification.

You lot'll start from the basics as an absolute beginner to the language and and then advance to more circuitous subjects such as Object Oriented Programming, Functional Programming, Data Structures, Algorithms, and helpful Debugging techniques.

In the finish, you'll build v projects to put your skills to do.

Thanks for reading, and happy learning!



Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people become jobs as developers. Become started

dunhampalwas.blogspot.com

Source: https://www.freecodecamp.org/news/javascript-tolowercase-how-to-convert-a-string-to-lowercase-and-uppercase-in-js/

0 Response to "Write a Program That Reads a Line of Input as a String Then Prints Only Uppercase Lettters"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel