Click here to Skip to main content
15,896,606 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 
GeneralRe: Why not just prototype the trim functions on to the strings themselves? Pin
AspDotNetDev16-Feb-10 23:46
protectorAspDotNetDev16-Feb-10 23:46 
Riaan Lehmkuhl wrote:
Please read my previous reply about the burden on memory you are talking about.


You say there will only be one instance of the class created. Says who? Why not prevent the user from going overboard by using a prototype instead?

Riaan Lehmkuhl wrote:
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.


I'm not sure what you mean here. If you provide a prototype for StringHelper (not for String), you should have no problem using it.

Riaan Lehmkuhl wrote:
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.


Your code is very basic. It took me all of 10 seconds to scan over it.

Riaan Lehmkuhl wrote:
If you dont like it just post an alternate solution


I'm just trying to help you (and others who may read your tip/trick). Why not improve your tip/trick to make use of a prototype? I could do it myself, but I don't want to be responsible for something an author should be responsible for. There are those who like to make those types of edits to tips/tricks, but I'm not one of them (yet).
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.