- //--- Définition de la méthode toArray() comme méthode de la classe String.
- //--- spacer = on définit un séparateur comme variable.
- String.prototype.toArray = function(spacer) {
- //--- convertedString_ar = tableau temporaire pour contenir les données passées.
- var convertedString_ar = new Array();
- // --- initialChar = définition d'une variable pour l'indice de début de la méthode substr().
- var initialChar = 0;
- //--- Boucle chargée de passer les éléments de la chaine à convertir.
- //--- (Le fait de définir toArray comme méthode de la classe string
- //--- permet d'utiliser le mot clé this en référence à la chaine à passer.)
- for (var curentChar = 0; curentChar<this.length; curentChar++) {
- //--- Pour Array.push et String.substr, conf. l'aide de Flash.
- if (this.charAt(curentChar) == spacer) {
- //--- Lorsqu'on rencontre un séparateur, on ajoute la sous-chaine
- //--- extraite dans le tableau temporaire.
- convertedString_ar.push(this.substr(initialChar, curentChar-initialChar));
- initialChar = curentChar+1;
- }
- if (curentChar == this.length-1) {
- //--- Au dernier caractère, on extrait la dernière sous-chaine et on
- //--- retourne le tableau...
- convertedString_ar.push(this.substr(initialChar, curentChar));
- return convertedString_ar;
- //--- ... et on efface les traces de notre forfait.
- delete convertedString_ar;
- }
- }
- };
- //--- Utilisation :
- sampleString_str = "red|blue|yellow";
- sampleArray_ar = sampleString_str.toArray("|");
- //--- Vérification :
- for (var i = 0; i<sampleArray_ar.length; i++) {
- trace(sampleArray_ar[i]);
- }
- //---
- stop();
//--- Définition de la méthode toArray() comme méthode de la classe String.
//--- spacer = on définit un séparateur comme variable.
String.prototype.toArray = function(spacer) {
//--- convertedString_ar = tableau temporaire pour contenir les données passées.
var convertedString_ar = new Array();
// --- initialChar = définition d'une variable pour l'indice de début de la méthode substr().
var initialChar = 0;
//--- Boucle chargée de passer les éléments de la chaine à convertir.
//--- (Le fait de définir toArray comme méthode de la classe string
//--- permet d'utiliser le mot clé this en référence à la chaine à passer.)
for (var curentChar = 0; curentChar<this.length; curentChar++) {
//--- Pour Array.push et String.substr, conf. l'aide de Flash.
if (this.charAt(curentChar) == spacer) {
//--- Lorsqu'on rencontre un séparateur, on ajoute la sous-chaine
//--- extraite dans le tableau temporaire.
convertedString_ar.push(this.substr(initialChar, curentChar-initialChar));
initialChar = curentChar+1;
}
if (curentChar == this.length-1) {
//--- Au dernier caractère, on extrait la dernière sous-chaine et on
//--- retourne le tableau...
convertedString_ar.push(this.substr(initialChar, curentChar));
return convertedString_ar;
//--- ... et on efface les traces de notre forfait.
delete convertedString_ar;
}
}
};
//--- Utilisation :
sampleString_str = "red|blue|yellow";
sampleArray_ar = sampleString_str.toArray("|");
//--- Vérification :
for (var i = 0; i<sampleArray_ar.length; i++) {
trace(sampleArray_ar[i]);
}
//---
stop();