PDA

View Full Version : Is there a ExtJS function to capitalize initial letters of all words in a string?



psubedi2010
30 Nov 2010, 7:22 AM
to format string of words as following example shows:

"JAMES BOND" format to "James Bond"
"James BOND" format to "James Bond"
"I HAVE a great Title" format to "I Have A Great Title"

Animal
30 Nov 2010, 7:50 AM
I have a few nice String extras which I find useful:



Ext.apply(String.prototype, (function() {
function uc(str, p1){
return p1.toUpperCase();
}
function lc(str, p1){
return p1.toLowerCase();
}
var camelRe = /-([a-z])/g,
titleRe = /((?:\s|^)[a-z])/g,
capsRe = /^([a-z])/,
decapRe = /^([A-Z])/,
leadAndTrailWS = /^\s*([^\s]*)?\s*/,
result;

return {

leftPad : function (val, size, ch) {
result = String(val);
if(!ch) {
ch = " ";
}
while (result.length < size) {
result = ch + result;
}
return result;
},

camel: function(s) {
return this.replace(camelRe, uc);
},

title: function(s) {
return this.replace(titleRe, uc);
},

capitalize: function() {
return this.replace(capsRe, uc);
},

decapitalize: function() {
return this.replace(decapRe, lc);
},

startsWith: function(prefix) {
return this.substr(0, prefix.length) == prefix;
},

endsWith: function(suffix) {
var start = this.length - suffix.length;
return (start > -1) && (this.substr(start) == suffix);
},

equalsIgnoreCase: function(other) {
return (this.toLowerCase() == other.toLowerCase());
},

/**
* Remove leading and trailing whitespace.
*/
normalize: function() {
return leadAndTrailWS.exec(this)[1] || '';
},

/**
* Case insensitive String equality test
*/
equalsIgnoreCase: function(other) {
return (this.toLowerCase() == (String(other).toLowerCase()));
}
};
})());


Then use



"JAMES BOND".toLowerCase().title()

Condor
30 Nov 2010, 8:00 AM
No there isn't, but you could add it, e.g.

Ext.util.Format.capitalize = (function(){
var re = /(^|[^\w])([a-z])/g,
fn = function(m, a, b) {
return a + b.toUpperCase();
};
return function(v){
return v.toLowerCase().replace(re, fn);
}
})();

snipeg
21 Jan 2012, 7:28 AM
Hi, you don't need JS to do this, in fact you shouldn't handle the presentation code through js unless there is no other way to do it. use text-transform: capitalize instead.

http://www.w3schools.com/cssref/pr_text_text-transform.asp

Cheers,

Guille

mmcgrath
12 Feb 2015, 10:14 AM
Hello, I am trying to learn sencha. I am a c# MVC developer.

How would you be able to right align and left align with similar style? Also, do i just add this funtion to the end of (below code).

Ext.define(
'MyApp.controller.ReportsController', {
extend:
'Ext.app.Controller',