Bonjour,
Je recherche une fonction qui va m'eliminer les 3 derniers
caracteres de ce string :
"itemarray2[3]"
Comment puis je faire ?
a+
Version imprimable
Bonjour,
Je recherche une fonction qui va m'eliminer les 3 derniers
caracteres de ce string :
"itemarray2[3]"
Comment puis je faire ?
a+
bonjour,
tout simplement :
Code:
1
2
3 var data ="itemarray2[3]"; alert(data.substring(0, data.length-3));
le vrai équivalent de substring (JAVA, .Net, ...) en JS est "substr"
Une autre proposition (car je suspecte qu'il puisse y avoir un jour
plus de trois caractères à supprimer dans cette chaine, par exemple
itemarray2[10]
Code:
1
2 var data ="itemarray2[3]"; alert(data.split('[')[0]);
un autre propositon ;)
Code:
1
2
3 var reg=/\[\d+\]/ var chaine="itemarray2[3]" alert(chaine.replace(reg,''))
En fait il existe substr() et substring(). Ces deux méthodes fonctionnent différemment :
Code:stringObject.substr(start,length)
Citation:
start Required. Where to start the extraction. Must be a numeric value
length Optional. How many characters to extract. Must be a numeric value.
Code:stringObject.substring(start,stop)
Citation:
start : Required. Where to start the extraction. Must be a numeric value
stop : Optional. Where to stop the extraction. Must be a numeric value
Et je crois que substr() ne fonctionne pas avec IE.
Sources :
substr()
substring()