Native JavaScript-string [3]

Source: Internet
Author: User

First of all, I apologize for the late update of the column.

I'm not talking much about the excuses ......

String comparison

 

= Equal
! = Not equal
=== Absolutely equal
! = Not absolutely equal
> Greater
> = Greater than or equal
< Less
<= Less than or equal

 

 

Equal or not

 

VaR str1 = "JavaScript"; var str2 = "JavaScript"; document. write ("str1 =" + str1); document. write ("<br/>"); document. write ("str2 =" + str2); document. write ("<br/>"); If (str1 = str2) {document. write ("str1 and str2 are equal! ");} Else {document. Write (" str1 and str2 are not equal! ");}

Result:

 


Absolutely equal

"=" And "! = "Not only the data content, but also the data type.

 

 
VaR str1 = "JavaScript"; var str2 = new string ("JavaScript"); document. write ("str1 content:" + str1 + ", str1 data type:" + typeof (str1); document. write ("<br/>"); document. write ("str2 content:" + str2 + ", str2 data type:" + typeof (str2); document. write ("<br/>"); If (str1 = str2) {document. write ("str1 and str2 are absolutely equal! ");} Else {document. Write (" str1 and str2 are not absolutely equal! ");}


Result:

 

Greater than or less
 
VaR str1 = "ABC"; var str2 = "ABC"; var str3 = "BCD"; if (str1> str2) {document. write ("str1 greater than str2");} else {document. write ("str1 less than str2");} document. write ("<br/>"); If (str2> str3) {document. write ("str2 greater than str3");} else {document. write ("str2 less than str3 ");}

Result:
In the string comparison, the following letters are greater than the first letters, and the lower-case letters are greater than the upper-case letters.Use the built-in JavaScript function localecompare for comparisonLocalecompare is a wonderful thing. It depends on the sorting of the default region settings in the local system. You can just take a look at it.

 
VaR str1 = "ABC"; var str2 = "ABC"; if (str1.localecompare (str2) = 0) {document. Write ("str1 and str2 are equal! ");} If (str1.localecompare (str2) =-1) {document. Write (" str1 before str2! ");} If (str1.localecompare (str2) = 1) {document. Write (" str1 after str2! ");}

Result: Str1 is behind str2!

Now, let's write it here!

My Sina Weibo: X -- Zhang

Column address: Native Javascript

Thank you!

Text/X -- Zhang

 

 

Related Article

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.