DevKB
Web Development Knowledge Base
HOME | TOP 20 | WRITE AN ARTICLE |
Sections :



RSS RSS Feed

You too, please publish your useful code snippets in any programming language :
write an article !


Valid HTML 4.0 Transitional

Plateforme d'envoi de gros fichiers en ligne
Dépannage site web
Blog infogérance
Hébergement e-mail

Olivier Ligny - - 11/03/2008 - vue 17194 fois

Fonction trim() en JavaScript pour enlever les espaces de début et de fin de chaine

Voici une fonction javascript trim() pour enlever les espaces blancs en début et en fin de chaine.

function trim(sString) {
    while (sString.substring(0,1) == ' ' || sString.substring(0,1) == '\t' || 
      sString.substring(0,1) == '\r' || sString.substring(0,1) == '\n')
    {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ' || 
      sString.substring(sString.length-1, sString.length) == '\t' || 
      sString.substring(sString.length-1, sString.length) == '\r' || 
      sString.substring(sString.length-1, sString.length) == '\n')
    {
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
}

 


Jean,ty - - 05/07/2008
Félicitation, ça marche bien ,ça fait ce que je voulait.
RANZI - 15/12/2008
Une grande partie des fonctions standards en PHP, sont disponibles en Javascript ici :
http://kevin.vanzonneveld.net/techblog/article/phpjs_licensing/
babadedakar - 03/06/2009
regarde ici aussi et tu fera un choix d'optimisation
http://anothergeekwebsite.com/fr/2007/03/trim-en-javascript
Guss - 27/05/2012
bonjour a toi, merci pour la gain de temps sur le nettoyage de mes retour ajax plus besoin de check mes fichier php et etre sur qu'un \n ne traine pas. grand merci
balti - - 26/09/2016
votre fonction trim() marche super bien


Write a comment :
Your name :     E-mail (optional) :

AntiSpam : what animal is visible on this picture ? :

Nos partenaires : iPhone 8 Cases & Protection