1

here is the question:

Imagine the String() constructor didn't exist. Create a constructor function, MyString(), that acts like String() as closely as possible. You're not allowed to use any built-in string methods or properties, and remember that String() doesn't exist. You can use this code to test your constructor:

I created constructor however I have no clue how to re-create split method, how to implement that functionality.

If you could give an idea how to implement split method, I would be grateful

function MyString(str) {
  var thisObj = this;
  var innerLength = 0;
  this.length;

  function updateLength() {
    innerLength = 0;
    for (var i = 0; str[i] != undefined; i++) {
      innerLength++;
      thisObj[i] = str[i];
    }
    thisObj.length = innerLength;
  }
  updateLength();
  this.toString = function() {
    return str;
  }

  this.charAt = function(i) {
    if (isNaN(parseInt(i))) {
      return this[0]
    } else {
      return this[i]
    }
  }

  this.concat = function(string) {
    str += string;
    updateLength();
  }

  this.slice = function(start, end) {
    var slicedString = "";
    if (start >= 0 && end >= 00) {
      for (start; start < end; start++) {
        slicedString += str[start];
      }
    }
    return slicedString;
  }

  this.reverse = function() {
    var arr = str.split("");
    arr.reverse();
    var reversedString = "",
      i;
    for (i = 0; i < arr.length; i++) {
      reversedString += arr[i];
    }
    return reversedString;
  }
}
var ms = new MyString("Hello, I am a string")
console.log(ms.reverse())
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Apart from being JAVA, here is an answer to use: https://stackoverflow.com/questions/2939691/i-want-to-split-string-without-using-split-function – mplungjan Aug 13 '17 at 15:51
  • Add an `.equals` method. With that and `slice` and a loop, you should be able to easily implement `split`. – Bergi Aug 13 '17 at 16:13

2 Answers2

0

Iterate and search:

 MyString.prototype.split = function(splitter){
  var tmp="", result = [];
  for(var i = 0; i < this.length; i++){
   for(var offset = 0; offset < this.length && offset < splitter.length;offset++){
     if(this[i+offset] !== splitter[offset]) break;
   }
   if(offset === splitter.length){
     result.push( tmp );
     tmp="";
     i += offset -1;
  }else{
     tmp+=this[i];
  }
 }
 result.push(tmp);
 return result;
};

So now you can do:

 new MyString("testtest").split("t") //['','es','','','es','']

In action

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

You can convert the string to an array and use Array.prototype and RegExp.prototype methods.

  this.split = function(re) {
     var arr = Array.prototype.slice.call(this.toString());
     if (re === "") {
       return arr
     } 
     if (re === " ") {
       return arr.filter(function(el) {
         return /[^\s]/.test(el)
       })
     }
     if (/RegExp/.test(Object.getPrototypeOf(re).constructor)) {
       var regexp = re.source;
       return arr.filter(function(el) {
         return regexp.indexOf(el) === -1
       })
     }
  }

function MyString(str) {
  var thisObj = this;
  var innerLength = 0;
  this.length;

  function updateLength() {
    innerLength = 0;
    for (var i = 0; str[i] != undefined; i++) {
      innerLength++;
      thisObj[i] = str[i];
    }
    thisObj.length = innerLength;
  }
  updateLength();
  this.toString = function() {
    return str;
  }
  
  this.split = function(re) {
     var arr = Array.prototype.slice.call(this.toString());
     if (re === "") {
       return arr
     } 
     if (re === " ") {
       return arr.filter(function(el) {
         return /[^\s]/.test(el)
       })
     }
     if (/RegExp/.test(Object.getPrototypeOf(re).constructor)) {
       var regexp = re.source;
       return arr.filter(function(el) {
         return regexp.indexOf(el) === -1
       })
     }
  }

  this.charAt = function(i) {
    if (isNaN(parseInt(i))) {
      return this[0]
    } else {
      return this[i]
    }
  }

  this.concat = function(string) {
    str += string;
    updateLength();
  }

  this.slice = function(start, end) {
    var slicedString = "";
    if (start >= 0 && end >= 00) {
      for (start; start < end; start++) {
        slicedString += str[start];
      }
    }
    return slicedString;
  }

  this.reverse = function() {
    var arr = str.split("");
    arr.reverse();
    var reversedString = "",
      i;
    for (i = 0; i < arr.length; i++) {
      reversedString += arr[i];
    }
    return reversedString;
  }
}
var ms = new MyString("Hello, I am a string")
console.log(ms.split(""), ms.split(" "), ms.split(/l/))
guest271314
  • 1
  • 15
  • 104
  • 177