0

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?

2 Answers 2

3

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"
Sign up to request clarification or add additional context in comments.

1 Comment

@sadmicrowave: yes, that would work fine. As a general rule I would stick to using slice or substring, substr isn't part of the ECMAScript standard and its implementation differs between browsers.
2

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

1 Comment

Note that the substr() is not part of the ECMAScript standard and its implementation varies between browsers. Although (0, 3) as arguments would work the same across implementations, I prefer to avoid substr() altogether.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.