Click here to Skip to main content
15,896,526 members
Articles / Programming Languages / Javascript
Tip/Trick

JavaScript string helper class

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
16 Feb 2010CPOL 26.4K   15
Save the following code to a .js file or add to an existing one you might have:function stringHelper() { this.isNullOrEmpty = function(str) { if (null == str) return true; str = this.trim(str); return str.length == 0; } this.trim =...
Save the following code to a .js file or add to an existing one you might have:

C#
function stringHelper() {
    this.isNullOrEmpty = function(str) {
        if (null == str) return true;
        str = this.trim(str);
        return str.length == 0;
    }
    
    this.trim = function(str, chars) {
        return this.ltrim(this.rtrim(str, chars), chars);
    }
 
    this.ltrim = function(str, chars) {
        chars = chars || "\\s";
        return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
    }
    
    this.rtrim = function (str, chars) {
        chars = chars || "\\s";
        return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
    }
    this.return2br = function (str) {
        if (this.isNullOrEmpty(str)) return "";
        return str.replace(/(\r\n|\r|\n)/g, "<br />");
    }
}
var StringHelper = new stringHelper();


To use, just use the StringHelper class instance, e.g.

C#
StringHelper.trim("    this is some text   ");

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Codely
South Africa South Africa
Me, a disorder of the brain that results in a disruption in a person's thinking, mood, and ability to relate to others.

Comments and Discussions

 
QuestionWhy not just prototype the trim functions on to the strings themselves? Pin
Helbrax16-Feb-10 9:24
Helbrax16-Feb-10 9:24 
AnswerRe: Why not just prototype the trim functions on to the strings themselves? Pin
Riaan Lehmkuhl16-Feb-10 10:30
professionalRiaan Lehmkuhl16-Feb-10 10:30 
GeneralRe: Why not just prototype the trim functions on to the strings themselves? Pin
AspDotNetDev16-Feb-10 13:33
protectorAspDotNetDev16-Feb-10 13:33 
AnswerRe: Why not just prototype the trim functions on to the strings themselves? Pin
Riaan Lehmkuhl16-Feb-10 20:05
professionalRiaan Lehmkuhl16-Feb-10 20:05 
GeneralRe: Why not just prototype the trim functions on to the strings themselves? Pin
AspDotNetDev16-Feb-10 22:56
protectorAspDotNetDev16-Feb-10 22:56 
AnswerRe: Why not just prototype the trim functions on to the strings themselves? Pin
Riaan Lehmkuhl16-Feb-10 23:36
professionalRiaan Lehmkuhl16-Feb-10 23:36 
Please read my previous reply about the burden on memory you are talking about.

Also using a prototype would not always work with the isNullOrEmpty method because the object that's passed in might not have been initialized as a String object. The prototype property of constructor functions, is used by the new operator, when it creates the new object instance.

This post was all about string manipulation, bet you didn't even read the code beyond the fact that I didn't use the prototype object.
If you dont like it just post an alternate solution Poke tongue | ;-P that's challenge by the way. Let's share in stead of complaining. Wink | ;)
GeneralRe: Why not just prototype the trim functions on to the strings themselves? Pin
AspDotNetDev16-Feb-10 23:46
protectorAspDotNetDev16-Feb-10 23:46 
RantRe: Why not just prototype the trim functions on to the strings themselves? Pin
Riaan Lehmkuhl17-Feb-10 1:17
professionalRiaan Lehmkuhl17-Feb-10 1:17 
GeneralRe: Why not just prototype the trim functions on to the strings themselves? Pin
Helbrax17-Feb-10 3:28
Helbrax17-Feb-10 3:28 
GeneralRe: Why not just prototype the trim functions on to the strings themselves? Pin
Riaan Lehmkuhl17-Feb-10 6:08
professionalRiaan Lehmkuhl17-Feb-10 6:08 
RantRe: Why not just prototype the trim functions on to the strings themselves? [modified] Pin
Riaan Lehmkuhl17-Feb-10 8:14
professionalRiaan Lehmkuhl17-Feb-10 8:14 
GeneralRe: Why not just prototype the trim functions on to the strings themselves? Pin
AspDotNetDev17-Feb-10 9:25
protectorAspDotNetDev17-Feb-10 9:25 
GeneralRe: Why not just prototype the trim functions on to the strings themselves? Pin
Riaan Lehmkuhl17-Feb-10 9:59
professionalRiaan Lehmkuhl17-Feb-10 9:59 
GeneralRe: Why not just prototype the trim functions on to the strings themselves? Pin
Helbrax24-Feb-10 8:30
Helbrax24-Feb-10 8:30 
GeneralRe: Why not just prototype the trim functions on to the strings themselves? Pin
AspDotNetDev24-Feb-10 8:51
protectorAspDotNetDev24-Feb-10 8:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.