我有
var id="ctl03_Tabs1";
使用JavaScript,如何获得最后五个字符或最后一个字符?
我有
var id="ctl03_Tabs1";
使用JavaScript,如何获得最后五个字符或最后一个字符?
当前回答
这里有两个例子,告诉你总是最后一个字符
var id=“ctl03_Tabs1”; console.log(id.charAt(id.length - 1)); console.log(id[id.length - 1]);
其他回答
性能
今天2020.12.31我在Chrome v87、Safari v13.1.2和Firefox v84上的MacOs HighSierra 10.13.6上执行测试,以获得最后N个字符的大小写(最后一个字母大小写的结果,为了清晰起见,我在单独的答案中给出)。
结果
适用于所有浏览器
基于切片的解决方案D是最快或最快的 解G最慢
细节
我执行2个测试用例:
当字符串有10个字符-你可以在这里运行它 当字符串有1M字符-你可以在这里运行它
下面的代码片段给出了解决方案 一个 B C D E F G(我)
//https://stackoverflow.com/questions/5873810/how-can-i-get-last-characters-of-a-string // https://stackoverflow.com/a/30916653/860099 function A(s,n) { return s.substr(-n) } // https://stackoverflow.com/a/5873890/860099 function B(s,n) { return s.substr(s.length - n) } // https://stackoverflow.com/a/17489443/860099 function C(s,n) { return s.substring(s.length - n, s.length); } // https://stackoverflow.com/a/50395190/860099 function D(s,n) { return s.slice(-n); } // https://stackoverflow.com/a/50374396/860099 function E(s,n) { let i = s.length-n; let r = ''; while(i<s.length) r += s.charAt(i++); return r } // https://stackoverflow.com/a/17489443/860099 function F(s,n) { let i = s.length-n; let r = ''; while(i<s.length) r += s[i++]; return r } // my function G(s,n) { return s.match(new RegExp(".{"+n+"}$")); } // -------------------- // TEST // -------------------- [A,B,C,D,E,F,G].map(f=> { console.log( f.name + ' ' + f('ctl03_Tabs1',5) )}) This shippet only presents functions used in performance tests - it not perform tests itself!
这里是chrome的示例结果
性能
今天2020.12.31我在Chrome v87、Safari v13.1.2和Firefox v84上的MacOs HighSierra 10.13.6上执行测试,以获得最后一个字符大小写(最后N个字母大小写结果,为了清晰起见,我在单独的答案中给出)。
结果
适用于所有浏览器
解D E F是非常快或最快的 解G H是最慢的
细节
我执行2个测试用例:
当字符串有10个字符-你可以在这里运行它 当字符串有1M字符-你可以在这里运行它
下面的代码片段给出了解决方案 一个 B C D E F G (my) H (my)
//https://stackoverflow.com/questions/5873810/how-can-i-get-last-characters-of-a-string // https://stackoverflow.com/a/30916653/860099 function A(s) { return s.substr(-1) } // https://stackoverflow.com/a/5873890/860099 function B(s) { return s.substr(s.length - 1) } // https://stackoverflow.com/a/17489443/860099 function C(s) { return s.substring(s.length - 1, s.length); } // https://stackoverflow.com/a/50395190/860099 function D(s) { return s.slice(-1); } // https://stackoverflow.com/a/50374396/860099 function E(s) { return s.charAt(s.length-1); } // https://stackoverflow.com/a/17489443/860099 function F(s) { return s[s.length-1]; } // my function G(s) { return s.match(/.$/); } // my function H(s) { return [...s].pop(); } // -------------------- // TEST // -------------------- [A,B,C,D,E,F,G,H].map(f=> { console.log( f.name + ' ' + f('ctl03_Tabs1') )}) This shippet only presents functions used in performance tests - it not perform tests itself!
这里是chrome的示例结果
假设你将子字符串与另一个字符串的结尾进行比较,并使用结果作为布尔值,你可以扩展string类来完成这一点:
String.prototype.endsWith = function (substring) {
if(substring.length > this.length) return false;
return this.substr(this.length - substring.length) === substring;
};
允许您执行以下操作:
var aSentenceToPonder = "This sentence ends with toad";
var frogString = "frog";
var toadString = "toad";
aSentenceToPonder.endsWith(frogString) // false
aSentenceToPonder.endsWith(toadString) // true
没有必要使用substr方法来获取字符串的单个字符!
以Jamon Holmgren为例,我们可以改变substr方法,简单地指定数组的位置:
var id = "ctl03_Tabs1";
var lastChar = id[id.length - 1]; // => "1"
我实际上有以下问题,这是我如何通过上述答案的帮助解决它,但不同的方法提取id形成一个输入元素。
我已附上输入字段与
id="rating_element-<?php echo $id?>"
并且,当按钮单击时,我想提取的id(这是数字)或php id($id)仅。
这就是我所做的。
$('.rating').on('rating.change', function() {
alert($(this).val());
// console.log(this.id);
var static_id_text=("rating_element-").length;
var product_id = this.id.slice(static_id_text); //get the length in order to deduct from the whole string
console.log(product_id );//outputs the last id appended
});