Strings as arrays

Discussion in 'Javascript' started by Tim Streater, Mar 6, 2007.

  1. Tim Streater

    Tim Streater Guest

    Safari and FF seem to allow this:

    var wiggy = "ABCD";

    ch = wiggy[2]; // ch will contain the character 'C'

    however my JS book seems to insist that I do this:

    ch = wiggy.charAt(2);

    and indeed doesn't appear to mention the first method at all.

    Since for my particular purpose I want to treat the string as an array
    of single characters, I prefer the first method rather than the second.
    Is there any reason not to pursue this approach?

    Thanks,

    -- tim
     
    Tim Streater, Mar 6, 2007
    #1
    1. Advertising

  2. Tim Streater

    Tom Cole Guest

    On Mar 6, 9:25 am, Tim Streater <> wrote:
    > Safari and FF seem to allow this:
    >
    > var wiggy = "ABCD";
    >
    > ch = wiggy[2]; // ch will contain the character 'C'
    >
    > however my JS book seems to insist that I do this:
    >
    > ch = wiggy.charAt(2);
    >
    > and indeed doesn't appear to mention the first method at all.
    >
    > Since for my particular purpose I want to treat the string as an array
    > of single characters, I prefer the first method rather than the second.
    > Is there any reason not to pursue this approach?
    >
    > Thanks,
    >
    > -- tim


    Only if you wish to make your application inaccessible to IE users. It
    returns [undefined] to them.
     
    Tom Cole, Mar 6, 2007
    #2
    1. Advertising

  3. Tim Streater

    Elegie Guest

    Tim Streater wrote:

    Hi,

    > Since for my particular purpose I want to treat the string as an array
    > of single characters, I prefer the first method rather than the second.
    > Is there any reason not to pursue this approach?


    An excellent one: it is not part of ECMAScript 262, and as such, is not
    something browser vendors have to implement.

    If you don't want to use the 'chatAt' method, one alternative could be
    to transform your string into an array, using the 'split' method.


    Kind regards,
    Elegie.
     
    Elegie, Mar 6, 2007
    #3
  4. Tim Streater

    Tim Streater Guest

    In article <45ed7e8a$0$464$>,
    Elegie <> wrote:

    > Tim Streater wrote:
    >
    > Hi,
    >
    > > Since for my particular purpose I want to treat the string as an array
    > > of single characters, I prefer the first method rather than the second.
    > > Is there any reason not to pursue this approach?

    >
    > An excellent one: it is not part of ECMAScript 262, and as such, is not
    > something browser vendors have to implement.


    Hmm, yes, I rather thought that might be the case, but thanks for
    confirming it.

    > If you don't want to use the 'charAt' method, one alternative could be
    > to transform your string into an array, using the 'split' method.


    I'll probably just stick with charAt. I haven't started implementing
    this part of my pages yet, but I want to have something I can treat as
    an array while manipulating it, but pass as a single entity between
    pages using something like:

    document.forms['xxx'].timeslots.value = wiggy;


    where I also have:

    <form name='xxx'>
    <input type=hidden name=timeslots>
    </form>


    (timeslots will go into a mysql table as char(64)).

    -- tim
     
    Tim Streater, Mar 6, 2007
    #4
  5. Tim Streater

    Tim Streater Guest

    In article <>,
    "Tom Cole" <> wrote:

    > On Mar 6, 9:25 am, Tim Streater <> wrote:
    > > Safari and FF seem to allow this:
    > >
    > > var wiggy = "ABCD";
    > >
    > > ch = wiggy[2]; // ch will contain the character 'C'
    > >
    > > however my JS book seems to insist that I do this:
    > >
    > > ch = wiggy.charAt(2);
    > >
    > > and indeed doesn't appear to mention the first method at all.
    > >
    > > Since for my particular purpose I want to treat the string as an array
    > > of single characters, I prefer the first method rather than the second.
    > > Is there any reason not to pursue this approach?
    > >
    > > Thanks,
    > >
    > > -- tim

    >
    > Only if you wish to make your application inaccessible to IE users. It
    > returns [undefined] to them.


    Round here, I'd probably get applause for that :)

    -- tim
     
    Tim Streater, Mar 6, 2007
    #5
  6. Tim Streater

    Tim Streater Guest

    In article <45edc6b6$0$28177$>,
    Michael White <> wrote:

    > Tim Streater wrote:
    >
    > > In article <45ed7e8a$0$464$>,
    > > Elegie <> wrote:
    > >
    > >
    > >>Tim Streater wrote:
    > >>
    > >>Hi,
    > >>
    > >>
    > >>>Since for my particular purpose I want to treat the string as an array
    > >>>of single characters, I prefer the first method rather than the second.
    > >>>Is there any reason not to pursue this approach?
    > >>
    > >>An excellent one: it is not part of ECMAScript 262, and as such, is not
    > >>something browser vendors have to implement.

    > >
    > >
    > > Hmm, yes, I rather thought that might be the case, but thanks for
    > > confirming it.
    > >
    > >
    > >>If you don't want to use the 'charAt' method, one alternative could be
    > >>to transform your string into an array, using the 'split' method.

    > >
    > >
    > > I'll probably just stick with charAt. I haven't started implementing
    > > this part of my pages yet, but I want to have something I can treat as
    > > an array while manipulating it, but pass as a single entity between
    > > pages using something like:
    > >
    > > document.forms['xxx'].timeslots.value = wiggy;

    >
    > document.forms['xxx'].timeslots.value = "abc".split("");
    >
    > Mick


    Mick,

    Could you expand on this a little? Is there something actually wrong (in
    the sense e.g. that it is non-standard, or some other issue) with it?
    What I need is for the hidden variable timeslots in the form below to
    get the value of the string wiggy.

    Thanks -- tim

    > > where I also have:
    > >
    > > <form name='xxx'>
    > > <input type=hidden name=timeslots>
    > > </form>
    > >
    > >
    > > (timeslots will go into a mysql table as char(64)).
    > >
    > > -- tim


    -- tim
     
    Tim Streater, Mar 7, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandra Stehman
    Replies:
    5
    Views:
    30,905
    Chris Smith
    Jun 17, 2004
  2. Bill Reyn
    Replies:
    3
    Views:
    2,341
    Bob Hairgrove
    Jun 22, 2004
  3. Mantorok Redgormor

    initializing arrays of arrays

    Mantorok Redgormor, Sep 10, 2003, in forum: C Programming
    Replies:
    4
    Views:
    589
  4. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    810
    Malcolm
    Jun 24, 2006
  5. Philipp
    Replies:
    21
    Views:
    1,186
    Philipp
    Jan 20, 2009
Loading...

Share This Page