2012-12-06 14 views

risposta

14

È possibile utilizzare la combinazione di [sottostringa()] [1] e [lastIndexOf()] [2] per ottenere l'ultimo elemento.

str = "~test content ~thanks ok ~fine";  
 
strFine =str.substring(str.lastIndexOf('~')); 
 
console.log(strFine);

È possibile usare [split()] [4] per convertire la stringa di matrice e ottenere l'elemento in ultimo indice, ultimo indice è length of array - 1 come matrice è indice di base zero .

str = "~test content ~thanks ok ~fine";  
 
arr = str.split('~'); 
 
strFile = arr[arr.length-1]; 
 
console.log(strFile);

O, semplicemente chiamare pop di matrice ottenuto dopo la divisione

str = "~test content ~thanks ok ~fine";  
 
console.log(str.split('~').pop());

+0

Grazie adil .... – Neel

+0

il primo metodo non funziona davvero con il mio percorso, mantiene l'ultima "\" nel nome del file .. ma il secondo è azzeccato. thks! – StinkyCat

5

Basta usare pianura JavaScript:

var str = "This is ~test content ~thanks ok ~fine"; 
var parts = str.split("~"); 
var what_you_want = parts.pop(); 
// or, non-destructive: 
var what_you_want = parts[parts.length-1]; 
Problemi correlati