Javascript

Javascript

Javascript or Jquery doesn't have the follwoing String functions which are available in other programming languages like Java, C#.Net etc.

  • 1. endsWith
  • 2. startsWith
  • 3. contians

    • But these functionalities can be implemented using workarounds.


      endsWith implementation in Javascript


      1. Cross Browser Solution :
      	String.prototype.endsWith = function(suffix) {
      		return this.indexOf(suffix, this.length - suffix.length) !== -1;
      	};
      	
      or
      	String.prototype.endsWith = function (s) {
      	  return this.length >= s.length && this.substr(this.length - s.length) == s;
      	}
      	
      or
      	String.prototype.endsWith = function(str)
      	{
      		var lastIndex = this.lastIndexOf(str);
      		return (lastIndex != -1) && (lastIndex   str.length == this.length);
      	}
      	

      Advantages :
      • Doesn't create a substring
      • Uses native indexOf function for fastest results
      • Skip unnecessary comparisons using the second parameter of indexOf to skip ahead
      • Works in Internet Explorer
      • NO Regex complications

      2. Standalone version :

      if you don't like stuffing things in native data structure's prototypes, :

      		function endsWith(str, suffix) {
      			return str.indexOf(suffix, str.length - suffix.length) !== -1;
      		}
      		

      3. Using Regular expression :
      		/#$/.test(str)
      		

      will work on all browsers, doesn't require monkey patching String, and doesn't require scanning the entire string as lastIndexOf does when there is no match.

      If you want to match a constant string that might contain regular expression special characters, such as '$', then you can use the following:

      		function makeSuffixRegExp(suffix, caseInsensitive) {
      		  return new RegExp(
      			  String(suffix).replace(/[$%()* .?[]{|}]/g, "$&")   "$",
      			  caseInsensitive ? "i" : "");
      		}
      		

      and then you can use it like this

      makeSuffixRegExp("a[complicated]*suffix*").test(str)


      startsWith implementation in Javascript


      1. Cross Browser Solution :
      		if (typeof String.prototype.startsWith != 'function') {
      		  // see below for better implementation!
      		  String.prototype.startsWith = function (str){
      			return this.indexOf(str) == 0;
      		  };
      		}
      		

      indexOf will be inefficient for large strings, its complexity is O(N). For a constant-time solution (O(1)), substring or String.prototype.slice needs to be used, which behaves similarly (note that I don't recommend using the substr, because it's inconsistent between implementations (most notably on JScript) )

      		if (typeof String.prototype.startsWith != 'function') {
      		  String.prototype.startsWith = function (str){
      			return this.slice(0, str.length) == str;
      		  };
      		}
      		

      The difference between substring and slice is basically that slice can take negative indexes, to manipulate characters from the end of the string, for example you could write the counterpart endsWith method by:

      		if (typeof String.prototype.endsWith != 'function') {
      		  String.prototype.endsWith = function (str){
      			return this.slice(-str.length) == str;
      		  };
      		}
      		

      2. Standalone version :

      if you don't like stuffing things in native data structure's prototypes, :

      		function endsWith(data, input) {
      			return (data.substring(0, input.length) === input);
      		}
      		

      3. Using Regular expression :
      		(/^He/).test('Hello world')
      		

      contains implementation in Javascript

      1. Prototype implementation :
      		String.prototype.contains = function(it) { return this.indexOf(it) != -1; };
      		
      2. Standalone version :
      		var s = "foo";
      		alert(s.indexOf("oo") !== -1);
      		indexOf returns the position of the string in the other string. If not found, it will return -1.
      		

      References :

      https://developer.mozilla.org/
      stackoverflow
      stackoverflow
      stackoverflow

      KU6QWD4ABWPY