Data una stringa del tipo var
s = "Stefano;Carlo;Nicola"
vorrei estrarre l'ultimo elemento della lista:
var v = ultimoelemento //v = "Nicola"
in modo che
s = "Stefano;Carlo"
cioè vorrei fare una specie di POP
Mi dareste una mano?
Data una stringa del tipo var
s = "Stefano;Carlo;Nicola"
vorrei estrarre l'ultimo elemento della lista:
var v = ultimoelemento //v = "Nicola"
in modo che
s = "Stefano;Carlo"
cioè vorrei fare una specie di POP
Mi dareste una mano?
Pietro
ciao pietro09,
io farei così
codice:var s = "Stefano;Carlo;Nicola" var arr = s.split(';'); var ultimoelemento = '' if(arr.length>0)ultimoelemento=arr[arr.length-1] arr.length-- s = arr.join(';')
O cosi....
codice:var s = "Stefano;Carlo;Nicola" var arr = s.split(';'); var ultimoelemento = arr.pop(); s = arr.join(';'); alert(ultimoelemento + " - " + s);
fikuz! Funzia anche su IE... una volta andava solo su NN... passa il tempo![]()
grazie badaze
![]()
vi ringrazio amici![]()
giusto per non mangiarmi la pappa già pronta ho fatto queste funzioni:
penso che vadano. Che ve ne sembra?
codice:function pop_lista_stringa(j) { var s = j.lista; var a = ""; var p = s.lastIndexOf(j.separatore); if(p != -1) { a = s.substr(p + 1); s = s.substring(0, p); } j.lista = s; j.ultimoelemento = a; } function push_lista_stringa(j) { var s = j.lista; var a = j.ultimoelemento; if(s.length == 0) { s = a; } else { s += j.separatore + a; } j.lista = s; }
Pietro
questa era la mia idea iniziale, ma non funzionava con la lista vuota (senza mettere un precontrollo intendevo)Originariamente inviato da badaze
O cosi....
codice:var s = "Stefano;Carlo;Nicola" var arr = s.split(';'); var ultimoelemento = arr.pop(); s = arr.join(';'); alert(ultimoelemento + " - " + s);![]()
Pietro
giusta domandaOriginariamente inviato da willybit
cos'è j? :master:![]()
a me serve implementare una funzione POP e PUSH. La POP deve restituire, sia l'ultimo elemento della lista, sia la lista senza l'ultimo elemento.
Ho pensato di passare alla funzione un oggetto che abbia le proprietà:
Lista, ultimoElemento e Separatore.
Non so se ci sia qualche modo migliore :master:![]()
![]()
In pratica, giusto per definire meglio il problema:
ho delle pagine asp che vengono chiamate in successione:
a-->b-->c-->d, dove può essere pure, a-->b-->d.
In ogni maschera vi sono due pulsanti, avanti e indietro. Una volta arrivati ad una qualunque pagina, ho la necessità di ripetere il percorso a ritroso.
Ho pensatodi gestire il tutto con una specie di STACK che si incrementa quando vado avanti e si decrementa quando torno indietro. Beh!
vi ho annoiato abbastanza.
Comunque, grazie delle risposte, sempre gentili e competenti![]()
Pietro
Puoi farti un oggetto che gestica le liste.
Ti do' un esempio (da completare)
codice:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Untitled</title> </head> <script language="JavaScript" type="text/javascript"> <!-- function List() { this.elements = new Array(); this.ind = -1; this.curSep = ";"; this.type = 'list'; // this.addElement = _addElement; //this.removeElement = _removeElement; this.push = _push; this.pop = _pop; this.asString = _asString; this.separator = _separator; this.listLength = _length; this.reset = _reset; this.next = _next; this.setList = _setList; } // function List() function _addElement(anElement,position) { if (position == null) { this.push(anElement); } } // function _addElement(anElement) function _reset() { this.ind = -1; } // function reset() function _next() { this.ind++; if (this.ind > this.listLength-1) { return null; } // if (this.ind >= this.length) return this.elements[this.ind]; } // function next() function _push(anElement) { this.elements.push(anElement); } // function push(anElement) function _pop(anElement) { return this.elements.pop(); } // function push(anElement) function _asString() { return this.elements.toString(); } // function _asString() function _separator(aSep) { this.curSep = aSep; } // function _separator(aSep) function _length() { return this.elements.length; } // function _length() function _setList(aString,aSep) { if (aSep == null) { sep = this.curSep; } else { sep = aSep; } // if (aSep == null) this.curSep = sep; this.elements = aString.split(sep); } // function _setList(aString,aSep) //--> </script> <body> <script language="JavaScript" type="text/javascript"> <!-- a = new List(); a.setList("a;b;c;d;e;f;g;h;i"); a.reset(); b = a.next(); while (b != null) { document.write(b); b = a.next(); } document.write(" "); document.write(a.asString()); document.write(" "); // a.push('k'); document.write(" "); document.write(a.asString()); document.write(" "); while (a.listLength() != 0) { a.pop(); document.write(a.asString()); document.write(" "); } //--> </script> </body> </html>
per badaze
Insomma, debbo proprio studiare![]()
Grazie, non ho mai affrontato la costruzione di una classe (o oggetto :master: ) in javascript, ed era ora di cominciare![]()
![]()
![]()
![]()
Pietro