Nodejs String Sub String subStrings()

Here you can find the source of subStrings()

Method Source Code

// 3. substrings//from  w  w  w  . ja  v a  2  s.  c o m

//Write a method substrings that will take a String and return an array containing each of its substrings.
// Don't repeat substrings. Example output: substrings("cat") => ["c", "ca", "cat", "a", "at", "t"].

String.prototype.subStrings = function(){
  const substringArray = [];

  for (var i = 0; i < this.length; i++){
    for (var j = i; j < this.length; j++){
      let substring = this.slice(i,j+1);
      if (!substringArray.includes(substring)){
        substringArray.push(substring);
      }
    }
  }
  return substringArray;
}

console.log("abc".subStrings());

Related

  1. subStringCounter(searchString)
    var text = 'this is  test test test we in are is';
    String.prototype.subStringCounter = function (searchString) {
        var position = 0,
            counter = 0;
        searchString = searchString.toLowerCase();
        while (this.indexOf(searchString, position) >= 0) {
            counter += 1;
            position = this.toLowerCase().indexOf(searchString, position) + 1;
        return counter;
    };
    console.log(text.subStringCounter('in'));
    console.log(text.subStringCounter('we'));
    
  2. subStrings()
    String.prototype.subStrings = function () {
      var substrings = [];
      for (var i = 0; i < this.length ; i++) {
        for (var j = i+1; j <= this.length; j++) {
          substrings.push(this.slice(i,j));
        };
      };
      return substrings;
    };
    ...
    
  3. subStrings()
    String.prototype.subStrings = function () {
      var subs = [];
      for (var i = 0; i < this.length; i++) {
        for (var j = i + 1; j < this.length; j++) {
          subs.push(this.slice(i, (j + 1)));
      return subs;
    };
    ...
    
  4. subStrings()
    String.prototype.subStrings = function () {
      var subs = [];
      for (var i = 0; i < this.length; i++) {
        for (var j = i+1; j <= this.length; j++) {
          subs.push(this.slice(i,j));
        };
      };
      var sorted = subs.sort();
      var uniques = [sorted[0]];
    ...
    
  5. subStrings()
    String.prototype.subStrings = function () {
      result = [];
      for (var beginning = 0; beginning < this.length; beginning++) {
        for (var end = beginning + 1; end < this.length + 1; end++) {
          result.push(this.slice(beginning, end));
      return result
    var a = "cat";
    var b = a.subStrings();
    console.log(b);
    
  6. subStrs()
    String.prototype.subStrs = function() {
      var output = [];
      for( var i = 0; i < this.length; i++ ) {
        for( var j = 0; j <= this.length; j++ ) {
          if(i < j) {
            output.push(this.substring(i,j));
        };
      };
    ...
    
  7. subst(replacements)
    String.prototype.subst = function(replacements) {
        return this.replace(/%[A-Z][A-Z0-9_]+%/g, function (placeholder) {
            return placeholder in replacements ? replacements[placeholder] : placeholder;
        });
    };
    
  8. subst(replacements)
    String.prototype.subst = function (replacements) {
        "use strict";
        return this.replace(/%[A-Z][A-Z0-9_]+%/g, function (placeholder) {
            return placeholder in replacements ? replacements[placeholder] : placeholder;
        });
    };
    
  9. substitute(object, regexp)
    String.prototype.substitute = function(object, regexp){
      return String(this).replace(regexp || (/\\?\{([^{}]+)\}/g), function(match, name){
        if (match.charAt(0) == '\\') return match.slice(1);
        return (object[name] !== null) ? object[name] : '';
      });
    };