描述
此方法接受一個字符串並將其與調用 String 對象進行比較。如果兩者相等,則返回 0;否則返回 -1 或 1。如果作為參數傳遞的字符串在根據本地瀏覽器語言的排序順序中排在最前麵,則返回 1;如果調用字符串按排序順序排在最前麵,則返回 -1。
用法
下麵給出的是localeCompare()JavaScript 的方法。我們可以使用 CoffeeScript 代碼中的相同方法。
string.localeCompare( param )
示例
下麵的例子演示了在 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.
相關用法
- CoffeeScript String lastIndexOf()用法及代碼示例
- CoffeeScript String slice()用法及代碼示例
- CoffeeScript String toUpperCase()用法及代碼示例
- CoffeeScript String toLocaleUpperCase()用法及代碼示例
- CoffeeScript String concat()用法及代碼示例
- CoffeeScript String match()用法及代碼示例
- CoffeeScript String substr()用法及代碼示例
- CoffeeScript String split()用法及代碼示例
- CoffeeScript String toLowerCase()用法及代碼示例
- CoffeeScript String charAt()用法及代碼示例
- CoffeeScript String search()用法及代碼示例
- CoffeeScript String toLocaleLowerCase()用法及代碼示例
- CoffeeScript String indexOf()用法及代碼示例
- CoffeeScript String charCodeAt()用法及代碼示例
- CoffeeScript Date setHours()用法及代碼示例
- CoffeeScript Date setUTCFullYear()用法及代碼示例
- CoffeeScript Date getDay()用法及代碼示例
- CoffeeScript Date getDate()用法及代碼示例
- CoffeeScript Date getYear()用法及代碼示例
- CoffeeScript Math pow()用法及代碼示例
注:本文由純淨天空篩選整理自 CoffeeScript String - localeCompare()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。