zaagan
1/29/2018 - 4:46 AM

String Manipulation

String Manipulation


// SOURCE: https://stackoverflow.com/questions/4068573/convert-string-to-pascal-case-aka-uppercamelcase-in-javascript

// SOURCE: https://stackoverflow.com/a/4068586
ToPascal: function (string) {

    string = string.replace(/(\w)(\w*)/g,
        function (g0, g1, g2) {
            return g1.toUpperCase() + g2.toLowerCase();
        });

    return string;
},


// Extend Prototype
// SOURCE: https://gist.github.com/jacks0n/268cbd2e4cebb5f7bb65a75810ef2820
 String.prototype.toPascalCase = function () {
     alert('prototype');
     return this.match(/[a-z]+/gi)
       .map(function (word) {
           return word.charAt(0).toUpperCase() + word.substr(1).toLowerCase();
       })
       .join('')
 }

// SOURCE: https://stackoverflow.com/questions/14880229/how-to-replace-a-substring-between-two-indices

String.prototype.replaceBetween = function(start, end, what) {
    return this.substring(0, start) + what + this.substring(end);
};

"The Hello World Code!".replaceBetween(4, 9, "Hi");
// >> "The Hi World Code!"

// ES6 Includes
var S = "fullweb";
S.includes("web");
S.includes("web", 3);  // <--- At position where to start


// RegExp .search()
var S = "fullweb";
S.search(/web/);


// RegExp .test()
var S = "fullweb";
/web/.test(S)


// Gold old' .indexOf()
var S = "fullweb";
S.indexOf("web");
S.indexOf("web", 3); // <--- Start point