INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

first 2 characters of a string

first 2 characters of a string

(OP)
how whould one get the first 2 characters of a string?  thanks

var d = "asdfasdfasdfasdf";
if (d.length>2)
{
  get first 2 characters here
}

RE: first 2 characters of a string

d.substring(0,1)

RE: first 2 characters of a string

(OP)
thnx

RE: first 2 characters of a string

jamert, this is a complete SW post but I figured I'd post it as an alternative to the above solution since it's what I always use.

The substring method requires 2 parameters.  The position of the first character in the string to pull, and the position of the last character in the string to pull.  However, at a glance I've always found the substring method to be confusing.  In marouanemeft's example above, it will return the first 2 characters in the string variable d.  However, if you look at the parameters passed (0 and 1), there is only a difference of 1.  Again, at a glance I find this confusing because I would expect it to only return 1 character (because of the difference between the two parameters).

Thankfully ECMA has provided us with an alternative.  The substr method accepts 2 parameters as well - the position of the first character in the string, and the length of the substring you wish to pull.  So, to modify marouanemeft's example to use the substr method:

CODE

d.substr(0, 2)

At a glance this is more intuitive.  You know immediately how long the substring will be that is returned.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close