CoffeeScript String - localeCompare()



Description

This method accepts a string and compares it with the calling String object. If both are equal, it returns 0; else it returns -1 or 1. And if the string passed as parameter comes first in the sorted order according to local browser language, it returns 1; and if the calling string comes first in the sorted order, -1 is returned.

Syntax

Given below is the syntax of localeCompare() method of JavaScript. We can use the same method from the CoffeeScript code.

string.localeCompare( param )

Example

The following example demonstrates the usage of localeCompare() method of JavaScript in CoffeeScript code. Save this code in a file with name 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."

Open the command prompt and compile the .coffee file as shown below.

c:\> coffee -c string_localecompare.coffee

On compiling, it gives you the following 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);

Now, open the command prompt again and run the CoffeeScript file as shown below.

c:\> coffee string_localecompare.coffee 

On executing, the CoffeeScript file produces the following 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.
coffeescript_strings.htm
Advertisements