Cadena CoffeeScript - localeCompare ()

Descripción

Este método acepta una cadena y la compara con el objeto String que llama. Si ambos son iguales, devuelve 0; de lo contrario, devuelve -1 o 1. Y si la cadena pasada como parámetro aparece primero en el orden ordenado según el idioma del navegador local, devuelve 1; y si la cadena de llamada aparece primero en el orden ordenado, se devuelve -1.

Sintaxis

A continuación se muestra la sintaxis de localeCompare()método de JavaScript. Podemos usar el mismo método del código CoffeeScript.

string.localeCompare( param )

Ejemplo

El siguiente ejemplo demuestra el uso del método localeCompare () de JavaScript en el código CoffeeScript. Guarde este código en un archivo con nombrestring_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."

Abre el command prompt y compile el archivo .coffee como se muestra a continuación.

c:\> coffee -c string_localecompare.coffee

Al compilar, le da el siguiente 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);

Ahora, abre el command prompt nuevamente y ejecute el archivo CoffeeScript como se muestra a continuación.

c:\> coffee string_localecompare.coffee

Al ejecutarse, el archivo CoffeeScript produce la siguiente salida.

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.