當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


CoffeeScript String localeCompare()用法及代碼示例


描述

此方法接受一個字符串並將其與調用 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 - localeCompare()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。