How To Add Trim Functionality To Javascript String Come aggiungere funzionalità a trim javascript stringa
Trim is a useful function available in languages like Java & PHP which removes the leading and traling whitespace(s) from a String. Trim è una funzione utile disponibile in linguaggi come Java e PHP che rimuove il leader e traling spazi bianchi (s) da una stringa. Unfortunately Javascript doesn’t natively provide trim functionality to the String object. Purtroppo il Javascript non nativamente trim fornire funzionalità per l'oggetto String. Fortunately there is a simple solution. Fortunatamente non vi è una soluzione semplice. Place the following code near the top of your Javascript file (or inline script) to add trim() functionality to all String objects. Inserisci il seguente codice nella parte superiore del vostro file JavaScript (o in linea di script) per aggiungere trim () a tutte le funzionalità String oggetti.
String prototype.trim = function() { a = this.replace(/^\s+/, ''); return a.replace(/\s+$/, ''); }; Prototype.trim stringa = function () (a = this.replace (/ ^ \ s + /,''); ritorno a.replace (/ \ s + $ /,'');); This underlines one of the beauty (or beast depending on who you ask) of Javascript - the ability to add functionality to an existing class, even native classes like String, by accessing its prototype. Ciò sottolinea uno dei bellezza (o bestia a seconda che si chiede) di Javascript - la capacità di aggiungere funzionalità a una classe esistente, anche nativo classi come String, accedendo il suo prototipo.
Now you can use trim() on any String in your code. Ora potete usare trim () su qualsiasi stringa nel codice.
Filed under Elencato sotto Javascript , Programming Programmazione , Web , Web 2.0 2,0 web | |
| |
RSS 2.0 RSS 2,0 | |
Trackback this Article | questo articolo |
Email this Article Invia questo articolo
You may also like to read Si può anche leggere come |




September 16th, 2007 at 11:50 pm 16 settembre 2007 a 11:50 pm
Hi Sir, Hi Signore,
The topic is very useful.But in this form it will not work.We have to put dot(.) after String instead of space. L'argomento è molto useful.But in questa forma non work.We hanno messo a punto (.) Dopo String invece di spazio.
String.prototype.trim = function() { String.prototype.trim = function () (
a = this.replace(/^s+/, ”); this.replace a = (/ ^ s + /, ");
return a.replace(/s+$/, ”); return a.replace (/ s + $ /, ");
}; );
December 27th, 2007 at 11:18 pm 27 dicembre 2007 a 11:18 pm
Sir, Egregio Signore,
I am striking with trim() option in Java script Mi colpisce con trim () opzione in Java script
I have a one string, Ho una stringa di uno,
A = “E:\ram\Collections\t1.jpeg” A = "E: \ ariete \ Collezioni \ t1.jpeg"
I need, Ho bisogno,
ans = “E:\ram\Collections\” only ans = "E: \ ariete \ Collezioni \" solo
Please kindly help me. Vi preghiamo gentilmente di aiutarmi. I can use the trim function but it not use. Sono in grado di utilizzare la funzione trim ma non uso.
Thanks & Regards Grazie & Cordiali saluti
TRHarihara sudhan TRHarihara sudhan