localeCompare()
优质
小牛编辑
140浏览
2023-12-01
描述 (Description)
此方法接受字符串并将其与调用String对象进行比较。 如果两者相等,则返回0; 否则返回-1或1.如果作为参数传递的字符串根据本地浏览器语言排在第一位,则返回1; 如果调用字符串以排序顺序排在第一位,则返回-1。
语法 (Syntax)
下面给出了JavaScript的localeCompare()方法的语法。 我们可以使用CoffeeScript代码中的相同方法。
string.localeCompare( param )
例子 (Example)
以下示例演示了CoffeeScript代码中JavaScript的localeCompare()方法的用法。 将此代码保存在名为string_localecompare.coffee的文件中
str1 = "This is beautiful string"
str2 = "This is beautiful string"
str3 = "abcd"
str4 = "xyz"
console.log "The value of str1:: "+str1
console.log "The value of str2:: "+str2
console.log "The value of str3:: "+str3
console.log "comparing the strings str1 and str2 ::"
index = str1.localeCompare str2
switch index
when 0 then console.log "Both strings are equal"
when 1 then console.log "Both strings are not equal and the string passed as parameter will be first in the sorted order."
when -1 then console.log "Both strings are not equal and the calling string object will be first in the sorted order."
console.log "comparing the strings str1 and str3 ::"
index = str1.localeCompare str3
switch index
when 0 then console.log "Both strings are equal"
when 1 then console.log "Both strings are not equal and the string passed as parameter will be first in the sorted order."
when -1 then console.log "Both strings are not equal and the calling string object will be first in the sorted order."
console.log "comparing the strings str1 and str4 ::"
index = str1.localeCompare str4
index = str1.localeCompare str3
switch index
when 0 then console.log "Both strings are equal"
when 1 then console.log "Both strings are not equal and the string passed as parameter will be first in the sorted order."
when -1 then console.log "Both strings are not equal and the calling string object will be first in the sorted order."
打开command prompt并编译.coffee文件,如下所示。
c:\> coffee -c string_localecompare.coffee
在编译时,它为您提供以下JavaScript。
// Generated by CoffeeScript 1.10.0
(function() {
var index, str1, str2, str3, str4;
str1 = "This is beautiful string";
str2 = "This is beautiful string";
str3 = "abcd";
str4 = "xyz";
console.log("The value of str1:: " + str1);
console.log("The value of str2:: " + str2);
console.log("The value of str3:: " + str3);
console.log("comparing the strings str1 and str2 ::");
index = str1.localeCompare(str2);
switch (index) {
case 0:
console.log("Both strings are equal");
break;
case 1:
console.log("Both strings are not equal and the string passed as parameter will be first in the sorted order.");
break;
case -1:
console.log("Both strings are not equal and the calling string object will be first in the sorted order.");
}
console.log("comparing the strings str1 and str3 ::");
index = str1.localeCompare(str3);
switch (index) {
case 0:
console.log("Both strings are equal");
break;
case 1:
console.log("Both strings are not equal and the string passed as parameter will be first in the sorted order.");
break;
case -1:
console.log("Both strings are not equal and the calling string object will be first in the sorted order.");
}
console.log("comparing the strings str1 and str4 ::");
index = str1.localeCompare(str4);
index = str1.localeCompare(str3);
switch (index) {
case 0:
console.log("Both strings are equal");
break;
case 1:
console.log("Both strings are not equal and the string passed as parameter will be first in the sorted order.");
break;
case -1:
console.log("Both strings are not equal and the calling string object will be first in the sorted order.");
}
}).call(this);
现在,再次打开command prompt并运行CoffeeScript文件,如下所示。
c:\> coffee string_localecompare.coffee
执行时,CoffeeScript文件生成以下输出。
The value of str1:: This is beautiful string
The value of str2:: This is beautiful string
The value of str3:: abcd
comparing the strings str1 and str2 ::
Both strings are equal
comparing the strings str1 and str3 ::
Both strings are not equal and the string passed as parameter will be first in the sorted order.
comparing the strings str1 and str4 ::
Both strings are not equal and the string passed as parameter will be first in the sorted order.