use*_*663 684 javascript
我有
var id="ctl03_Tabs1";
Run Code Online (Sandbox Code Playgroud)
使用JavaScript,我如何获得最后五个字符或最后一个字符?
Jam*_*ren 1026
编辑:正如其他人所指出的,使用slice(-5)而不是substr.但是,请参阅.split().pop()本答案底部的解决方案以获得另一种方法.
原始答案:
您将要使用.substr()与该.length属性结合的Javascript字符串方法.
var id = "ctl03_Tabs1";
var lastFive = id.substr(id.length - 5); // => "Tabs1"
var lastChar = id.substr(id.length - 1); // => "1"
Run Code Online (Sandbox Code Playgroud)
这将获取从id.length - 5开始的字符,并且由于省略了.substr()的第二个参数,因此继续到字符串的末尾.
您也可以.slice()像其他人在下面指出的那样使用该方法.
如果您只是想在下划线后找到字符,可以使用:
var tabId = id.split("_").pop(); // => "Tabs1"
Run Code Online (Sandbox Code Playgroud)
这会将字符串拆分为下划线上的数组,然后"弹出"数组中的最后一个元素(这是您想要的字符串).
Ter*_*nce 777
不要用.substr().请改用该.slice()方法,因为它是跨浏览器兼容的(请参阅IE).
const id = "ctl03_Tabs1";
console.log(id.slice(id.length - 5)); //Outputs: Tabs1
console.log(id.slice(id.length - 1)); //Outputs: 1Run Code Online (Sandbox Code Playgroud)
Zir*_*rak 59
获取最后一个字符很简单,因为您可以将字符串视为数组:
var lastChar = id[id.length - 1];
Run Code Online (Sandbox Code Playgroud)
要获取字符串的一部分,可以使用substr函数或substring函数:
id.substr(id.length - 1); //get the last character
id.substr(2); //get the characters from the 3rd character on
id.substr(2, 1); //get the 3rd character
id.substr(2, 2); //get the 3rd and 4th characters
Run Code Online (Sandbox Code Playgroud)
substr和之间的区别substring是如何处理第二个(可选)参数.在substr,它是索引中的字符数(第一个参数).在substring,它是字符切片应该结束的索引.
小智 50
您可以使用带有负起始位置的substr()方法来检索最后n个字符.例如,这得到了最后5:
var lastFiveChars = id.substr(-5);
Run Code Online (Sandbox Code Playgroud)
小智 30
const r = '6176958e92d42422203a3c58';
r.slice(-4)
Run Code Online (Sandbox Code Playgroud)
结果“3c58”
r.slice(-1)
Run Code Online (Sandbox Code Playgroud)
结果'8'
小智 23
以下脚本显示使用JavaScript获取字符串中最后5个字符和最后1个字符的结果:
var testword='ctl03_Tabs1';
var last5=testword.substr(-5); //Get 5 characters
var last1=testword.substr(-1); //Get 1 character
Run Code Online (Sandbox Code Playgroud)
输出:
Tabs1 //有5个字符
1 //有1个角色
Kei*_*van 13
一种方法是使用slice,如下所示:
var id="ctl03_Tabs1";
var temp=id.slice(-5);
Run Code Online (Sandbox Code Playgroud)
所以 的值temp将是"Tabs1"。
Substr 函数允许您使用减号来获取最后一个字符。
var string = "hello";
var last = string.substr(-1);
Run Code Online (Sandbox Code Playgroud)
它非常灵活。例如:
// Get 2 characters, 1 character from end
// The first part says how many characters
// to go back and the second says how many
// to go forward. If you don't say how many
// to go forward it will include everything
var string = "hello!";
var lasttwo = string.substr(-3,2);
// = "lo"
Run Code Online (Sandbox Code Playgroud)
检查子字符串函数.
要获得最后一个角色:
id.substring(id.length - 1, id.length);
Run Code Online (Sandbox Code Playgroud)
没有必要使用substr方法来获取字符串的单个字符串!
以Jamon Holmgren为例,我们可以改变substr方法并简单地指定数组位置:
var id = "ctl03_Tabs1";
var lastChar = id[id.length - 1]; // => "1"
Run Code Online (Sandbox Code Playgroud)
今天 2020.12.31 我在 Chrome v87、Safari v13.1.2 和 Firefox v84 上对 MacOs HighSierra 10.13.6 进行测试,以获取用于获取最后 N 个字符大小写的选择解决方案(最后一个字母大小写结果,为了清楚起见,我在单独的答案中提出)。
适用于所有浏览器
slice是快速还是最快我执行 2 个测试用例:
下面的片段展示了解决方案 A 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)
)})Run Code Online (Sandbox Code Playgroud)
This shippet only presents functions used in performance tests - it not perform tests itself!Run Code Online (Sandbox Code Playgroud)
以下是 chrome 的示例结果
const id = 'ctl03_Tabs1';
id.at(-1); // Returns '1'
Run Code Online (Sandbox Code Playgroud)
at支持负整数从最后一个字符串字符开始倒数。
文档:字符串/at
如果您只想要最后一个字符或已知位置的任何字符,您可以简单地将字符串作为数组!- 字符串在 javascript 中是可迭代的 -
Var x = "hello_world";
x[0]; //h
x[x.length-1]; //d
Run Code Online (Sandbox Code Playgroud)
然而,如果您需要的不仅仅是一个字符,那么使用 splice 是有效的
x.slice(-5); //world
Run Code Online (Sandbox Code Playgroud)
关于你的例子
"rating_element-<?php echo $id?>"
Run Code Online (Sandbox Code Playgroud)
要提取 id,您可以轻松使用 split + pop
Id= inputId.split('rating_element-')[1];
Run Code Online (Sandbox Code Playgroud)
这将返回 id,如果在 'rating_element' 之后没有 id,则返回 undefined :)
今天 2020 年 12 月 31 日,我在 Chrome v87、Safari v13.1.2 和 Firefox v84 上的 MacOs HighSierra 10.13.6 上执行测试,以选择获取最后一个字符大小写的解决方案(最后 N 个字母大小写结果,为了清楚起见,我在单独的答案中给出)。
对于所有浏览器
我执行了 2 个测试用例:
下面的代码片段提供了解决方案 A 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')
)})Run Code Online (Sandbox Code Playgroud)
This shippet only presents functions used in performance tests - it not perform tests itself!Run Code Online (Sandbox Code Playgroud)
这是 chrome 的示例结果
| 归档时间: |
|
| 查看次数: |
876121 次 |
| 最近记录: |