string 

Send to Kindle
home » snippets » javascript » string



Methods

Ref: String Methods

Method Description
indexOf(searchValue[, fromIndex]) Returns -1 if the value is not found.
split([separator][, limit])] separator can be a string or a regex.  If omitted, you get a array of one element containing the string.
substr(start[, length]) start can be negative and is equivalent to string.length + start. If abs(start) > string.length or length is less than 1, you get an empty string.
substring(indexA[, indexB]) indexA is inclusive and indexB is exclusive. If either is negative, they are treated as zero and if either is greater than the string length, they're set equal to the string length.

Gotchas

Ref: String primitives vs. instances

var s_prim = "foo";
var s_obj = new String(s_prim);

console.log(typeof s_prim); // "string"
console.log(typeof s_obj);  // "object"

console.log("===:", s_prim === s_obj); // false
console.log("=== + valueOf:", s_prim === s_obj.valueOf()); // true

Snippets

// gist
function isString(obj) {
  return Object.prototype.toString.call(obj) == '[object String]';
};

// capture global on init
var isString = (function() {
    var _toString = Object.prototype.toString;
    return function isString(obj) {
      return _toString.call(obj) == '[object String]';
    };
  })();