Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 783013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:28:42+00:00 2026-05-14T20:28:42+00:00

Ok – am trying to create a string library that contains a handful of

  • 0

Ok – am trying to create a string library that contains a handful of useful things missing from JavaScript. Here is what I have so far:

function $__STRING__$(in_string) { this.s = in_string; }
$__STRING__$.prototype = {
    uppercase:      function(){this.s = this.s.toUpperCase(); return this;},
    lowercase:      function(){this.s = this.s.toLowerCase(); return this;},
    trim:           function(){this.s = this.s.replace(/^\s+|\s+$/g,""); return this;},
    ltrim:          function(){this.s = this.s.replace(/^\s+/,""); return this;},
    rtrim:          function(){this.s = this.s.replace(/\s+$/,""); return this;},
    striptags:      function(){this.s = this.s.replace(/<\/?[^>]+(>|$)/g, ""); return this;},
    escapetags:     function(){this.s = this.s.replace(/</g,"<").replace(/>/g,">"); return this;},
    unescapetags:   function(){this.s = this.s.replace(/</g,"<").replace(/>/g,">"); return this;},
    underscorize:   function(){this.s = this.s.replace(/ /g,"_"); return this;},
    dasherize:      function(){this.s = this.s.replace(/ /g,"-"); return this;},
    spacify:        function(){this.s = this.s.replace(/_/g," "); return this;},
    left:           function(length){this.s = this.s.substring(length,0); return this;},
    right:          function(length){this.s = this.s.substring(this.s.length,this.s.length-length); return this;},
    shorten:        function(length){if(this.s.length<=length){return this.s;}else{this.left(this.s,length)+"..."; return this;}},
    mid:            function(start,length){return this.s.substring(start,(length+start));},
    _down:          function(){return this.s;},
    contains:       function(needle){if(this.s.indexOf(needle)!==-1){return true;}else{return false;}},
    startswith:     function(needle){if(this.left(this.s,needle.length)==needle){return true;}else{return false;}},
    endswith:       function(needle){if(this.right(this.s,needle.length)==needle){return true;}else{return false;};},
    toString:       function(){return this.s;}
}

function $E(in_string){return new $__STRING__$(in_string);}
String.prototype._enhance   = function(){return new $__STRING__$(this);};
String.prototype._up        = function(){return new $__STRING__$(this);};

It works fairly well, and I can chain commands etc.

I set it up so I can cast a string as an enhanced string these 2 ways:

$E('some string');
'some string'._enhance();

However, each time I want to use a built-in string method, I need to convert it back to a string first. So for now, I put in _down() and _up() methods like so:

alert( $E("hello man").uppercase()._down().replace("N", "Y")._up().dasherize() ); 
alert( "hello man"._enhance().uppercase()._down().replace("N", "Y")._up().dasherize() );

It works fine, but what I really want to do it be able to use all of the built-in functions a string can use. I realize I can just replicate each function inside my object, but I was hoping there was a simpler way.

So question is, is there an easy way to do that?

Thanks –

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:28:43+00:00Added an answer on May 14, 2026 at 8:28 pm

    You can loop through the methods in String.prototype:

    for(var name in String.prototype) {
        if (typeof String.prototype[name] !== "function") continue;
        if (name in $__STRING__$.prototype) continue;
    
        addToProto(name);
    }
    
    function addToProto(name) {
        $__STRING__$.prototype[name] = function() {
            this.s = String.prototype[name].apply(this.s, arguments);
            return this;
        };
    }
    

    Note that this will not handle methods like indexOf correctly; you could modify it like this:

    $__STRING__$.prototype[name] = function() {
        var retVal = String.prototype[name].apply(this.s, arguments);
        if (typeof retVal === "string") {
            this.s = retVal;
            return this;
        } else 
            return retVal;
    };
    

    Also, you should move your explicit methods to the prototype, like this:

     $__STRING__$.prototype.uppercase = function(){this.s = this.s.toUpperCase(); return this;};
    

    EDIT: The for .. in loop will not work correctly (the methods in String.prototype are not enumerable).

    Instead, you need to call addToProto manually for each method name, like this:

    addToProto('replace');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.