Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

Join Tek-Tips
*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 from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

first 2 characters of a stringHelpful Member! 

jamert (Programmer) (OP)
12 Feb 08 23:48
how whould one get the first 2 characters of a string?  thanks

var d = "asdfasdfasdfasdf";
if (d.length>2)
{
  get first 2 characters here
}
Helpful Member!  marouanemeft (Programmer)
12 Feb 08 23:51
d.substring(0,1)
jamert (Programmer) (OP)
12 Feb 08 23:57
thnx
kaht (Programmer)
13 Feb 08 9:45
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>

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!

Back To Forum

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