The comparison of Chinese characters involved in JavaScript

Source: Internet
Author: User
Tags collator first string intl locale sorts

In the use of js "= =" for string comparisons, found in the English case is OK, but in the Chinese comparison is not.

Online Search provides a workaround for using

Stringobject.localecompare (target)

To solve the Chinese comparison problem.

String_a.localecompare (string_b); /* returns:0:  exact match-1:  string_a < String_b 1:  * /

The use of this method is to determine whether two strings are equal in the current locale.

Microsoft information is as follows:

Http://msdn.microsoft.com/zh-cn/library/ie/62b7ahzy (v=vs.94). aspx

Localecompare Method (String) (JavaScript)

Determines whether two strings are equal in the current locale.

Parameters
Stringvar

Necessary. the first string to compare.

Stringexp

Necessary. the second string to compare.

Locales

Optional.An array of locale strings that contain one or more language or locale tags.If you have multiple locale strings, arrange them in descending order of precedence to ensure that the first entry is the preferred region location. If this argument is omitted, the JavaScript runtime's default locale is used. This parameter must conform to the BCP 47 standard; see the Intl.collator object for more information.

Options

Optional. An object that contains one or more attributes that specify the comparison options. See Intl.collator objects for more information.

Notes

For comparison strings, you can specify a String object or string literal.

locales and options parameters." * Starting with Internet Explorer 11, localecompare   Internal use of   Intl.collator   object, adding support for  locales  and options  parameters.   intl.collator and Intl.Collator.compare . " > For more information on these parameters, see <  Intl.Collator  and  intl.collator.compare.

Important

locales and options parameters is not supported in all document modes and browser versions. " >locales  and  options  parameters are not supported in all document models and browser versions.   For more information, see the Requirements section.

Stringvar and stringexp and returns one of the following results, depend ing on the sort order of the system default locale: "> Localecompare   method to  stringexp  perform a locale-sensitive string comparison and return one of the following results, depending on the sort order of the system default locale:

    • stringvar sorts before stringexp . " >-1, if  stringvar  is ranked before  stringexp .

    • stringvar sorts after stringexp . " >+1, if  stringvar  is ranked behind  stringexp .

    • If two strings are equal, then 0 (0).

Example

The following code shows how to use localecompare.

Javascript
var str1 = "Def"; var str2 = "abc" document.write (Str1.localecompare (str2) + "<br/>");//Output:1var STR3 = "Ghi";d OC Ument.write (Str1.localecompare (STR3) + "<br/>");//Output: -1var STR4 = "def";d Ocument.write (str1.localecompare (STR4)); /output:0

The following code shows how to use localecomparewith the German (Germany) locale.

Javascript
var str1 = "a"; var str2 = "B";d Ocument.write (Str1.localecompare (str2, "de-de"));//output//-1

The following example shows how to use a locale that has a German (German) locale and a specified German phone book sort order to set the Localecomparefor a specific extension. This example demonstrates locale-specific differences.

Javascript
var arr = ["OK", "ad", "AF", "a"];d ocument.write (Arr[0].localecompare (arr[1], "DE-DE-U-CO-PHONEBK"));  Returns 1document.write (Arr[0].localecompare (arr[2], "DE-DE-U-CO-PHONEBK"));  Returns-1document.write (Arr[0].localecompare (arr[3], "DE-DE-U-CO-PHONEBK"));  Returns 1document.write (Arr[0].localecompare (arr[1], "de-de"));  Returns-1document.write (Arr[0].localecompare (arr[2], "de-de"));  Returns-1document.write (Arr[0].localecompare (arr[3], "de-de"));  Returns 1
Requirements

Supported in the following document modes: Quirks, Internet Explorer 6 standard mode, Internet Explorer 7 Standard mode, Internet Explorer 8 standard mode, Internet Explorer 9 standard mode, INT Ernet Explorer 10 Standard mode and Internet Explorer 11 standard mode. In addition, it is also supported in store apps (Windows 8 and Windows Phone 8.1). See version information.

Locales and options parameters:

This is supported in Internet Explorer 11 standard document mode. In addition, it is also supported in store apps (Windows 8.1 and Windows Phone 8.1). See version information.

Not supported in the following document modes: Quirks, Internet Explorer 6 standard mode, Internet Explorer 7 Standard mode, Internet Explorer 8 standard mode, Internet Explorer 9 standard mode, and I Nternet Explorer 10 Standard mode. Not supported in Windows 8.

The comparison of Chinese characters involved in JavaScript

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.