Click here to Skip to main content
15,887,477 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.3K   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 
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.