sadmicrowave
sadmicrowave

Reputation: 40912

jQuery/javascript delete suffix of variable

I have a variable set to retrieve the id of a specific element which 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?

Upvotes: 0

Views: 678

Answers (2)

Karl Johan
Karl Johan

Reputation: 4022

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

Upvotes: 2

Andy E
Andy E

Reputation: 344595

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"

Upvotes: 3

Related Questions