Javascript 如何获取字符串的最后一个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5873810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get last characters of a string
提问by user695663
I have
我有
var id="ctl03_Tabs1";
Using JavaScript, how might I get the last five characters or last character?
使用 JavaScript,我如何获得最后五个字符或最后一个字符?
回答by Jamon Holmgren
EDIT: As others have pointed out, use slice(-5)
instead of substr
.However, see the .split().pop()
solution at the bottom of this answer for another approach.
编辑:正如其他人所指出的,使用slice(-5)
代替substr
. 但是,请参阅.split().pop()
此答案底部的解决方案以了解另一种方法。
Original answer:
原答案:
You'll want to use the Javascript string method .substr()
combined with the .length
property.
您需要结合使用 Javascript 字符串方法.substr()
和.length
属性。
var id = "ctl03_Tabs1";
var lastFive = id.substr(id.length - 5); // => "Tabs1"
var lastChar = id.substr(id.length - 1); // => "1"
This gets the characters starting at id.length - 5 and, since the second argument for .substr() is omitted, continues to the end of the string.
这将获取从 id.length - 5 开始的字符,并且由于 .substr() 的第二个参数被省略,因此继续到字符串的末尾。
You can also use the .slice()
method as others have pointed out below.
您也可以使用.slice()
下面其他人指出的方法。
If you're simply looking to find the characters after the underscore, you could use this:
如果您只是想查找下划线后的字符,则可以使用以下命令:
var tabId = id.split("_").pop(); // => "Tabs1"
This splits the string into an array on the underscore and then "pops" the last element off the array (which is the string you want).
这会将字符串拆分为下划线上的数组,然后从数组中“弹出”最后一个元素(这是您想要的字符串)。
回答by Terence
Don't use .substr()
. Use the .slice()
method instead because it is cross browser compatible (see IE).
不要使用.substr()
. 请改用该.slice()
方法,因为它是跨浏览器兼容的(请参阅 IE)。
const id = "ctl03_Tabs1";
console.log(id.slice(id.length - 5)); //Outputs: Tabs1
console.log(id.slice(id.length - 1)); //Outputs: 1
回答by user682701
You can use the substr() methodwith a negative starting position to retrieve the last n characters. For example, this gets the last 5:
您可以使用带有负起始位置的substr() 方法来检索最后 n 个字符。例如,这将获得最后 5 个:
var lastFiveChars = id.substr(-5);
回答by Zirak
Getting the last character is easy, as you can treat strings as an array:
获取最后一个字符很容易,因为您可以将字符串视为数组:
var lastChar = id[id.length - 1];
To get a section of a string, you can use the substrfunction or the substringfunction:
要获取字符串的一部分,可以使用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
The difference between substr
and substring
is how the second (optional) parameter is treated. In substr
, it's the amount of characters from the index (the first parameter). In substring
, it's the index of where the character slicing should end.
substr
和之间的区别在于substring
如何处理第二个(可选)参数。在 中substr
,它是索引中的字符数(第一个参数)。在 中substring
,它是字符切片结束位置的索引。
回答by tamil
Following script shows the result for get last 5 characters and last 1 character in a string using JavaScript:
以下脚本显示了使用 JavaScript 在字符串中获取最后 5 个字符和最后 1 个字符的结果:
var testword='ctl03_Tabs1';
var last5=testword.substr(-5); //Get 5 characters
var last1=testword.substr(-1); //Get 1 character
Output :
输出 :
Tabs1 // Got 5 characters
1 // Got 1 character
Tabs1 // 有 5 个字符
1 // 得到 1 个字符
回答by Chewpers
Check out the substring function.
查看子字符串函数。
To get the last character:
获取最后一个字符:
id.substring(id.length - 1, id.length);
回答by Nello Ollen
There is no need to use substr
method to get a single char of a string!
不需要使用substr
方法来获取字符串的单个字符!
taking the example of Jamon Holmgren we can change substr method and simply specify the array position:
以 Jamon Holmgren 为例,我们可以更改 substr 方法并简单地指定数组位置:
var id = "ctl03_Tabs1";
var lastChar = id[id.length - 1]; // => "1"
回答by Adam Whateverson
The Substr function allows you to use a minus to get the last character.
Substr 函数允许您使用减号来获取最后一个字符。
var string = "hello";
var last = string.substr(-1);
It's very flexible. For example:
它非常灵活。例如:
// 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"
回答by Keivan
One way would be using slice
, like follow:
一种方法是使用slice
,如下所示:
var id="ctl03_Tabs1";
var temp=id.slice(-5);
so the value of temp
would be "Tabs1"
.
所以 的值temp
将是"Tabs1"
。
回答by Zalaboza
If you just want the last character or any character at know position you can simply trat string as an array! - strings are iteratorable in javascript -
如果您只想要最后一个字符或已知位置的任何字符,您可以简单地将字符串作为数组!- 字符串在 javascript 中是可迭代的 -
Var x = "hello_world";
x[0]; //h
x[x.length-1]; //d
Yet if you need more than just one character then use splice is effective
然而,如果您需要的不仅仅是一个字符,那么使用 splice 是有效的
x.slice(-5); //world
Regarding your example
关于你的例子
"rating_element-<?php echo $id?>"
To extract id you can easily use split + pop
要提取 id,您可以轻松使用 split + pop
Id= inputId.split('rating_element-')[1];
This will return the id, or undefined if no id was after 'rating_element' :)
这将返回 id,如果在 'rating_element' 之后没有 id,则返回 undefined :)