Use JavaScript to change the case sensitivity of strings. use javascript to change the case sensitivity of strings.
JavaScript provides two methods to convert a string to uppercase or lowercase, so that "hello" can be changed to "HELLO" or "NOT" can be changed 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 have created a question program and the question is "Who was the first American to win the Tour De France ?" You may use code similar to the following to check the answers of the respondents:
var correctAnswer='Greg LeMond';var response=prompt('Who was the first American to win the Tour De8France?','');if(response==correctAnswer){//correct}else{//incorrect}
The answer is Greg LeMond. However, if the respondent entered Greg Lemond, what would happen? The condition looks like this: 'greg lemmond '= 'greg lemmond '. Because JavaScript is case-sensitive, the lower-case letter 'M' in Lemond does not match the 'M' in LeMond. Therefore, the respondent may get an incorrect answer. If you press the Caps key and enter greg lemond, the same result is returned.
To solve this problem, we can convert both strings into the same case and then compare them:
if(response.toUpperCase()==correctAnswer.toUpperCase()){//correct}else{//incorrect}
In this example, the Condition Statement converts both the answer and the correct answer of the subscriber into uppercase letters. Therefore, 'greg lemmond 'is changed to 'greg lemmond ', in addition, 'greg lemmond 'is changed to 'greg lemmond '.
To obtain all lowercase strings, use the toLowerCase () method as follows:
var answer='Greg LeMond';alert(answer.toLowerCase());//'greg lemond'
Note that none of these methods actually change the Strings actually stored in the variables. They only return the strings in uppercase or lowercase. Therefore, in the above example, even after the prompt appears, answer still contains 'greg LeMond '(they return some other values ).
ToLowerCase () and toUpperCase () are two classic methods. They are derived from the same name method in java. lang. String. The toLocaleLowerCase () and toLocaleUpper () methods are implemented for specific regions. For some regions, the method for the region is the same as the general method, but some languages apply special rules for Unicode case-sensitivity conversion, at this time, you must use the region-specific method to ensure correct conversion.
The following are 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”
In the above Code, both toLocaleUpperCase () and toUpperCase () Return "hello world", just as if toLocaleLowerCase () and toLowerCase () both return "hello world. In general, it is safer to use a region-specific method without knowing that your code will run in that language environment.
The above is all the content of this article. I hope you will like it.