JavaScript change the case of a string _javascript tips

Source: Internet
Author: User
Tags locale lowercase

JavaScript provides two ways to convert a string to all uppercase or lowercase, so that you can change "hello" to "hello" or "not" to "not". You may ask, why? Converts letters in a string to the same case, which makes it easier to compare two strings. For example, suppose you create a problem program, and a question is "who is the" the American to win the Tour De France? You may use code similar to the following to check the answers to the respondents:

var correctanswer= ' Greg LeMond ';
var response=prompt (' Who is the ' the ' the ' the ' the ' the ' American to win the Tour De8
? ');
if (response==correctanswer) {
//correct
}else{
//incorrect
}

The answer is Greg LeMond, but what happens if the person who participates in the answer enters Greg LeMond? Conditions look something like this: ' greg LeMond ' = ' Greg LeMond '. Because JavaScript is case-sensitive, the lowercase letter ' m ' in LeMond does not match the ' m ' in LeMond, so the participant may get the wrong answer. If the participant presses the CAPS key and enters Greg LeMond, the result will be the same.

To solve this dilemma, we can convert two strings to the same case, and then compare them:

if (Response.touppercase () ==correctanswer.touppercase ()) {
//correct
}else{
//incorrect
}

In this case, the conditional statement converts the answer of the participant and the correct answer to uppercase letters, so ' Greg LeMond ' became ' Greg LeMond ' and ' Greg LeMond ' became ' Greg LeMond '.

To get all lowercase strings, use the toLowerCase () method as follows:

var answer= ' Greg LeMond ';
alert (Answer.tolowercase ());//' Greg LeMond '

Note that none of these methods really changes the strings that are actually stored in the variable, and they simply return the string in all uppercase or lowercase letters. So, in the example above, even after the hint appears, answer still contains ' Greg LeMond ' (They return some other value).

toLowerCase () and toUpperCase () are two classical methods that draw on the same method from Java.lang.String. The toLocaleLowerCase () and Tolocaleupper () methods are for specific locale implementations. For some regions, the approach to the region is the same as the common method, but a few languages apply special rules for Unicode case conversions, and a locale-specific approach is required to ensure that the correct transformations are implemented.

Here are a few examples:

var sv= "Hello World";
Alert (Sv.tolocaleuppercase ());//"Hello World"
alert (Sv.touppercase ());//"Hello World"
alert ( Sv.tolocalelowercase ())//"Hello World"
alert (Sv.tolowercase ());//"Hello World"

The above code uses toLocaleUpperCase () and toUpperCase () to return to "Hello World", just as the call to toLocaleLowerCase () and toLowerCase () all return to "Hello World" The same. In general, it is safer to use the area-specific approach without knowing that your code will run in that language.

The above mentioned is the entire content of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.