当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。