Created
March 22, 2015 08:12
-
-
Save erlinis/8f22b60b0d0f42129abb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function MyString(text){ | |
this.text = text; | |
this.length = function(){ | |
var len = 0; | |
for (var iter in this.text){ | |
len = len + 1; | |
} | |
return len; | |
}; | |
this.toString = function(){ | |
return this.text; | |
}; | |
this.valueOf = function(){ | |
return this.text; | |
}; | |
this.charAt = function(index){ | |
var idx = (Number(index))? (Number(index)): 0; | |
for (var iter in this.text){ | |
if(idx == iter){ | |
char = this.text[idx]; | |
} | |
} | |
return char; | |
}; | |
this.concat = function(newText){ | |
return this.text + newText; | |
}; | |
this.slice = function(start, end){ | |
var slicedText = ""; | |
end = (end <= -1) ? (text.length - 1): end; | |
for (var iter in this.text){ | |
if(iter >= start && iter < end){ | |
slicedText += this.text[iter]; | |
} | |
} | |
return slicedText; | |
}; | |
this.split = function(separator){ | |
var result = []; | |
var slice = ""; | |
for (var iter in this.text){ | |
if( this.text[iter] == separator){ | |
result.push(slice); | |
slice = ""; | |
}else{ | |
slice += this.text[iter]; | |
} | |
} | |
result.push(slice); | |
return result; | |
}; | |
this.reverse = function(){ | |
var stringArray = []; | |
for (var iter in this.text){ | |
stringArray.push(this.text[iter]); | |
} | |
return stringArray.reverse().join(""); | |
}; | |
} | |
var test = new MyString("hello"); | |
console.log(test.length()); | |
console.log(test.toString()); | |
console.log(test.charAt(1)); | |
console.log(test.charAt('2')); | |
console.log(test.charAt('e')); | |
console.log(test.concat(' world!')); | |
console.log(test.slice(1,3)); | |
console.log(test.slice(0,-5)); | |
console.log(test.split('e')); | |
console.log(test.split('l')); | |
console.log(test.reverse()); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment