localecompare
JavaScript | 字符串localeCompare()方法 (JavaScript | String localeCompare() Method)
The String.localeCompare() method in JavaScript is used to compare two string in JavaScript. The result of the comparison will be,
JavaScript中的String.localeCompare()方法用于比较JavaScript中的两个字符串。 比较的结果将是
Positive: str1 is lexicographically greater than str2.
正值 :在字典上str1比str2大 。
Zero: str1 is equal to str2.
零 : str1等于str2 。
Negative: str1 is lexicographically smaller than str2.
负数 :在字典上str1比str2小。
Syntax:
句法:
str1.localeCompare(str2);
Parameter(s):
参数:
The method accepts one parameter which is the string to be compared with the calling string.
该方法接受一个参数,该参数是要与调用字符串进行比较的字符串。
Return value:
返回值:
The return type of this method is number which is the result of the comparison.
此方法的返回类型是数字 ,它是比较的结果。
Browser support: Chrome, Internet explorer, Mozilla, Safari, Opera mini.
浏览器支持: Chrome,Internet Explorer,Mozilla,Safari,Opera mini。
Example 1:
范例1:
<script>
document.write("Hello".localeCompare("Hello") + "<br/>");
document.write("hello".localeCompare("hello") + "<br/>");
document.write("Hello".localeCompare("hello") + "<br/>");
document.write("hello".localeCompare("Hello") + "<br/>");
document.write("ABC".localeCompare("BCD") + "<br/>");
document.write("BCD".localeCompare("ABC") + "<br/>");
document.write("PREM".localeCompare("SHIVANG") + "<br/>");
</script>
Output
输出量
0
0
1
-1
-1
1
-1
Example 2:
范例2:
<script>
str1 = "include"
str2 = "Help"
result = str1.localeCompare(str2);
if (result) {
document.write(str1 + " is greater than " + str2 + "<br>");
}
</script>
Output
输出量
include is greater than Help
localecompare