开发者

jQuery/javascript delete suffix of variable

开发者 https://www.devze.com 2023-01-01 21:45 出处:网络
I have a variable set to retrieve the id of a specific element which开发者_StackOverflow中文版 is \"PubRecNum-1\" (the number is also variable and will change depending on the record number).What I\'m

I have a variable set to retrieve the id of a specific element which开发者_StackOverflow中文版 is "PubRecNum-1" (the number is also variable and will change depending on the record number). What I'm needing is a way to only take the first 3 letters of the variable "Pub" (needed for comparison purposes).

Any ideas?


You can get those letters using substring() or slice():

var str = "PubRecNum-1";
alert(str.substring(0, 3)); // -> "Pub"
alert(str.slice(0, 3)); // -> "Pub"


Javascript's substring() or substr() is used for exactly this

0

精彩评论

暂无评论...
验证码 换一张
取 消