Search

What the Quote?

"Listen up!!! For the last time!!! If false is not true then what is not true.. But if false is true then what is not true....You know what I mean, dude?"

Microsoft Boardroom

"If I were the chicken-giver-outer, you'd be covered in poultry."

Steven Rodgers

"Next week we're gonna powder-puff Monkey Butt."

Lawson Hise

« Javascript String.toProperCase | Main| Java is not my friend »

Update: Javascript String.toProperCase

Category show-n-tell thursday
I couldn't resist. Here's the updated Javascript function that propercases all words in a String, not just the first. Just insert a .push for each additional delimiter (i.e. quote, comma, pipe, etc.) you may wish to scan for, and the internal iteration function will uppercase the first character after each.

Object.extend(String.prototype, {
    toProperCase: function() {
        var strOriginalValue = this.toLowerCase();
        var strModifiedValue = strOriginalValue.slice(0,1).toUpperCase() + strOriginalValue.slice(1).toLowerCase();
        var arrDelimiters = new Array();
        arrDelimiters.push(' ');
        arrDelimiters.push('\\');
        arrDelimiters.push('(');
        arrDelimiters.each(
            function(strDelimiter) {
                var strTemporary = '';
                var strRemaining = strModifiedValue;
                var intDelimiterIndex = strRemaining.indexOf(strDelimiter);
                while (intDelimiterIndex > -1) {
                    var strBeforeDelimiter = strRemaining.slice(0,intDelimiterIndex);
                    var strAfterDelimiter = strRemaining.slice(intDelimiterIndex + 1);
                    strRemaining = strAfterDelimiter.slice(0,1).toUpperCase() + strAfterDelimiter.slice(1);
                    strTemporary += strBeforeDelimiter + strDelimiter;
                    intDelimiterIndex = strRemaining.indexOf(strDelimiter);
                }
                strModifiedValue = strTemporary + strRemaining;
            }
        );
        return strModifiedValue;
    }
});

Comments

Gravatar Image1 - Here's a non-prototype adaptation of a very compact, elegant solution that I found at { Link } (seems to be down at the moment, but it was working yesterday)

function toProperCase(str) {
return str.toLowerCase().replace(
/\w+/g,function(s){ return s.charAt(0).toUpperCase() + s.substr(1); }
)
}

Contact Me

Elsewhere

Assorted Linkage


Locations of visitors to this page