---
layout: page
title: "JavaScript strncasecmp function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/strncasecmp:542
- /functions/view/strncasecmp
- /functions/view/542
- /functions/strncasecmp:542
- /functions/542
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's strncasecmp

{% codeblock strings/strncasecmp.js lang:js https://raw.github.com/kvz/phpjs/master/functions/strings/strncasecmp.js raw on github %}
function strncasecmp (argStr1, argStr2, len) {
  // From: http://phpjs.org/functions
  // +   original by: Saulo Vallory
  // +      input by: Nate
  // +   bugfixed by: Onno Marsman
  // %          note: Returns < 0 if str1 is less than str2 ; > 0 if str1 is greater than str2 , and 0 if they are equal.
  // *     example 1: strncasecmp('Price 12.9', 'Price 12.15', 2);
  // *     returns 1: 0
  // *     example 2: strncasecmp('Price 12.09', 'Price 12.15', 10);
  // *     returns 2: -1
  // *     example 3: strncasecmp('Price 12.90', 'Price 12.15', 30);
  // *     returns 3: 8
  // *     example 4: strncasecmp('Version 12.9', 'Version 12.15', 20);
  // *     returns 4: 8
  // *     example 5: strncasecmp('Version 12.15', 'Version 12.9', 20);
  // *     returns 5: -8
  var diff, i = 0;
  var str1 = (argStr1 + '').toLowerCase().substr(0, len);
  var str2 = (argStr2 + '').toLowerCase().substr(0, len);

  if (str1.length !== str2.length) {
    if (str1.length < str2.length) {
      len = str1.length;
      if (str2.substr(0, str1.length) == str1) {
        return str1.length - str2.length; // return the difference of chars
      }
    } else {
      len = str2.length;
      // str1 is longer than str2
      if (str1.substr(0, str2.length) == str2) {
        return str1.length - str2.length; // return the difference of chars
      }
    }
  } else {
    // Avoids trying to get a char that does not exist
    len = str1.length;
  }

  for (diff = 0, i = 0; i < len; i++) {
    diff = str1.charCodeAt(i) - str2.charCodeAt(i);
    if (diff !== 0) {
      return diff;
    }
  }

  return 0;
}
{% endcodeblock %}

 - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/strings/strncasecmp.js)

Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are 
the closest match to this hashtable-like data structure. 

Please also note that php.js offers community built functions and goes by the 
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online 
functions that are far from perfect, in the hopes to spark better contributions. 
Do you have one? Then please just: 

 - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/strings/strncasecmp.js)

### Example 1
This code
{% codeblock lang:js example %}
strncasecmp('Price 12.9', 'Price 12.15', 2);
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
0
{% endcodeblock %}

### Example 2
This code
{% codeblock lang:js example %}
strncasecmp('Price 12.09', 'Price 12.15', 10);
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
-1
{% endcodeblock %}

### Example 3
This code
{% codeblock lang:js example %}
strncasecmp('Price 12.90', 'Price 12.15', 30);
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
8
{% endcodeblock %}


### Other PHP functions in the strings extension
{% render_partial _includes/custom/strings.html %}
