Comparison of Javascript in Chinese -- localecompare () method

Source: Internet
Author: User

Google many times only found on foreign websites have prompted how to compare Chinese, the original address: http://www.webdeveloper.com/forum/showthread.php? T = 9365

Prerequisites: unicode encoding is used. If other GBK encoding methods are used, this method should not work!

Localecompare () method

Definition and usage

Compares two strings in a specific local order.

Syntax
stringObject.localeCompare(target)
Parameters Description
Target String to be compared with stringobject in a specific local order.
Return Value

Number of the comparison result. If stringobject is smaller than target, localecompare () returns a number smaller than 0. If the value of stringobject is greater than target, this method returns a value greater than 0. If the two strings are equal or there is no difference according to the local sorting rules, this method returns 0.

Test 1:

VaR STR = "Beijing ";
VaR var2 = "Beijing ";
VaR res = Str. localecompare (var2 );
Console. Log (RES );

Output 0, equal

Test 2:

VaR STR = "Monday ";
VaR var2 = "Tuesday ";
VaR res = Str. localecompare (var2 );
Console. Log (RES );

Output 1

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.