2011-09-18 16 views
83

Come posso, usando Javascript, creare una funzione che taglierà la stringa passata come argomento, ad una lunghezza specificata, anche passata come argomento. Ad esempio:Javascript taglia una stringa alla lunghezza

var string = "this is a string"; 
var length = 6; 
var trimmedString = trimFunction(length, string); 

/* trimmedString should be: 
    "this is" */ 

Chi ha delle idee? Ho sentito qualcosa sull'uso della sottostringa, ma non ho capito bene.

+0

"questo è" = 7 caratteri, lo intendevi? –

+0

Come detto nella domanda, non sono troppo sicuro di come usare la sottostringa per farlo. Potresti per favore darmi un esempio? Naturalmente accetterò una risposta, se funziona;) @Interstellar_Coder oops, typo! –

risposta

192

Perché non usare solo la sottostringa ... string.substring(0, 7); Il primo argomento (0) è il punto di partenza. Il secondo argomento (7) è il punto finale (esclusivo). More info here.

var string = "this is a string"; 
var length = 7; 
var trimmedString = string.substring(0, length); 
+0

Grazie! Funziona perfettamente. La contrassegnerò come risposta non appena il tempo limite sarà passato! –

+13

E se vuoi le ellissi per le stringhe che superano la lunghezza massima (probabilmente più utili per un massimo maggiore): var string = "this is a string"; var length = 20; var trimmedString = string.length> length? string.substring (0, length - 3) + "...": string.substring (0, length); – Will

+2

Si noti che string.substr (inizio, lunghezza) si comporta in modo strano, restituirà vuoto se la lunghezza è maggiore della lunghezza della stringa. string.substring (start, end) funziona come previsto, cioè restituirà la stringa fino alla fine se non ci sono abbastanza caratteri per la fine data! – centic

21

Copia Will's commento in una risposta, perché ho trovato utile:

var string = "this is a string"; 
var length = 20; 
var trimmedString = string.length > length ? 
        string.substring(0, length - 3) + "..." : 
        string; 

Grazie Will.

E un jsfiddle per chi si prende cura https://jsfiddle.net/t354gw7e/ :)

+0

Nessun punto nell'uso di 'string.substring (0, lunghezza)' nel caso else, poiché la stringa è garantita per essere <= 20 caratteri in quel punto, basta usare 'stringa' . –

+0

Cheers, avete aggiornato la risposta –

6

perché non utilizzare un'estensione?

String.prototype.trim = function (length) { 
    return this.length > length ? this.substring(0, length) + "..." : this; 
} 

e usarlo come:

var stringObject= 'this is a verrrryyyyyyyyyyyyyyyyyyyyyyyyyyyyylllooooooooooooonggggggggggggsssssssssssssttttttttttrrrrrrrrriiiiiiiiiiinnnnnnnnnnnnggggggggg'; 
stringObject.trim(25) 

Caution:

Perché stiamo sovrascrivere prototipo di un oggetto standard .trim(). Ciò potrebbe causare comportamenti imprevisti in altre librerie e strumenti che è possibile utilizzare.

+1

Perché no? Perché stai sovrascrivendo il prototipo di un oggetto standard '.trim()'. Ciò potrebbe causare comportamenti imprevisti in altre librerie e strumenti che è possibile utilizzare. –

+0

@OlegBerman Certo, ha preso nota del tuo commento. – xameeramir

Problemi correlati