Skip to content

Instantly share code, notes, and snippets.

@zoghal
Created September 4, 2015 19:35
Show Gist options
  • Save zoghal/4917ac4b11e0ef2fe936 to your computer and use it in GitHub Desktop.
Save zoghal/4917ac4b11e0ef2fe936 to your computer and use it in GitHub Desktop.
JavaStrings methods
function StringUtil() {
this.reverseWords = function(str) {
var result = "";
var wordArray = str.split(" ");
for(var i = wordArray.length - 1; i >= 0; i--) {
result += wordArray[i] + " ";
}
return result.trim();
}
}
String.prototype.splitStr = function(delimiter) {
var stringArray = [];
var tempStr = "";
for(var i = 0; i < this.length; i++) {
if(this.charAt(i) === delimiter) {
stringArray.push(tempStr);
tempStr = "";
} else {
tempStr += this.charAt(i);
}
}
if(tempStr !== "") {
stringArray.push(tempStr);
}
return stringArray;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment