Indicates whether or not this string starts/ends with the specified string. - Node.js String

Node.js examples for String:Search

Description

Indicates whether or not this string starts/ends with the specified string.

Demo Code

/**//from www. j  a va2 s  .co m
 * Indicates whether or not this string starts with the specified string.
 * @param {Object} string
 */
String.prototype.startsWith = function(string){
    if (!string) 
        return false;
    return this.indexOf(string) == 0;
}

/**
 * Indicates whether or not this string ends with the specified string.
 * @param {Object} string
 */
String.prototype.endsWith = function(string){
    if (!string || string.length > this.length) 
        return false;
    return this.indexOf(string) == this.length - string.length;
}

Related Tutorials