CoffeeScript String - localeCompare ()

Descrizione

Questo metodo accetta una stringa e la confronta con l'oggetto String chiamante. Se entrambi sono uguali, restituisce 0; altrimenti restituisce -1 o 1. E se la stringa passata come parametro viene prima nell'ordine ordinato in base alla lingua del browser locale, restituisce 1; e se la stringa chiamante è la prima nell'ordine ordinato, viene restituito -1.

Sintassi

Di seguito è riportata la sintassi di localeCompare()metodo di JavaScript. Possiamo usare lo stesso metodo dal codice CoffeeScript.

string.localeCompare( param )

Esempio

L'esempio seguente mostra l'utilizzo del metodo localeCompare () di JavaScript nel codice CoffeeScript. Salva questo codice in un file con nomestring_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."

Apri il command prompt e compila il file .coffee come mostrato di seguito.

c:\> coffee -c string_localecompare.coffee

Durante la compilazione, ti dà il seguente 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);

Ora apri il file command prompt di nuovo ed eseguire il file CoffeeScript come mostrato di seguito.

c:\> coffee string_localecompare.coffee

All'esecuzione, il file CoffeeScript produce il seguente output.

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.