如果我們將字符串存儲在變量中,這是一個微不足道的問題。但出於好奇,我需要了解如何實現這一點,而不需要在變量中存儲字符串?Javascript:獲取字符串的最後幾個字符
// To get first few characters of a string, we can do something like this:
var x = "amangupta".substring(0,7); //amangup
// How to get last 7 characters
var x = "amangupta".substring(this.length - 7, this.length); // does not work, looking for similar approach
var str = "amangupta";
var x = str.substring(str.length - 7, str.length); // will work fine
這是一個重複問題請到這個答案http:// stackoverflow.com/a/15473635/5452965 – codtex