我有
var id="ctl03_Tabs1";使用JavaScript,如何获得最后五个字符或最后一个字符?
我有
var id="ctl03_Tabs1";使用JavaScript,如何获得最后五个字符或最后一个字符?
Answers:
编辑:正如其他人指出的那样,请使用slice(-5)代替substr。但是,请参阅.split().pop()此答案底部的解决方案以了解另一种方法。
原始答案:
您将需要结合使用Javascript字符串方法.substr()和.length属性。
var id = "ctl03_Tabs1";
var lastFive = id.substr(id.length - 5); // => "Tabs1"
var lastChar = id.substr(id.length - 1); // => "1"
这将获得从id.length-5开始的字符,并且由于省略了.substr()的第二个参数,因此将继续到字符串的末尾。
您也可以使用.slice()下面其他人指出的方法。
如果您只是想在下划线后找到字符,则可以使用以下命令:
var tabId = id.split("_").pop(); // => "Tabs1"这会将字符串拆分为下划线的一个数组,然后将最后一个元素“弹出”到数组中(这是您想要的字符串)。
.split().pop()仍然是每秒500万次操作。在性能问题之前,这不是性能问题。;)
                    slice是一个更好的选择。
                    slice或substr使用数字(int),请先将其转换为字符串。例如myVar.toString().slice(-5)
                    不要使用.substr()。请使用此.slice()方法,因为它与跨浏览器兼容(请参阅IE)。
const id = "ctl03_Tabs1";
console.log(id.slice(id.length - 5)); //Outputs: Tabs1
console.log(id.slice(id.length - 1)); //Outputs: 1slice()参考:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…–
                    _Tabs15怎么办?slice(-5)只会抓abs15?分流/流行音乐在各种长度下的效果都更好……
                    您可以将substr()方法用于起始位置为负的位置来检索最后n个字符。例如,这得到最后5个:
var lastFiveChars = id.substr(-5);获取最后一个字符很容易,因为您可以将字符串视为数组:
var lastChar = id[id.length - 1];要获取字符串的一部分,可以使用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
substr和之间的区别在于substring如何处理第二个(可选)参数。在中substr,它是索引中的字符数(第一个参数)。在中substring,它是字符切片应在何处结束的索引。
无需使用substr方法来获取字符串的单个字符!
以Jamon Holmgren为例,我们可以更改substr方法并只需指定数组位置:
var id = "ctl03_Tabs1";
var lastChar = id[id.length - 1]; // => "1"Substr函数允许您使用减号来获取最后一个字符。
var string = "hello";
var last = string.substr(-1);非常灵活。例如:
// 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"如果您只希望最后一个字符或任何已知位置的字符,您都可以将字符串当作数组来处理!-字符串在javascript中是可迭代的-
Var x = "hello_world";
 x[0];                    //h
 x[x.length-1];   //d但是,如果您只需要一个字符,那么使用拼接是有效的
x.slice(-5);      //world关于你的例子
"rating_element-<?php echo $id?>"要提取ID,您可以轻松使用split + pop
Id= inputId.split('rating_element-')[1];这将返回id;如果在'rating_element'之后没有id,则返回undefined:
假设您将子字符串与另一个字符串的末尾进行比较,并将结果用作布尔值,则可以扩展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如果它是字符串中的最后一个字符,则它将删除逗号。
var str = $("#ControlId").val();
if(str.substring(str.length-1)==',') {
  var stringWithoutLastComma = str.substring(0,str.length-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
        });我相信这会奏效。
var string1="myfile.pdf"
var esxtenion=string1.substr(string1.length-4)的值extension将是".pdf"