jQuery/javascript delete suffix of variable
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
精彩评论