Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Strings as arrays

Reply
Thread Tools

Strings as arrays

 
 
Tim Streater
Guest
Posts: n/a
 
      03-06-2007
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
 
Reply With Quote
 
 
 
 
Tom Cole
Guest
Posts: n/a
 
      03-06-2007
On Mar 6, 9:25 am, Tim Streater <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
 
Elegie
Guest
Posts: n/a
 
      03-06-2007
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.

 
Reply With Quote
 
Tim Streater
Guest
Posts: n/a
 
      03-06-2007
In article <45ed7e8a$0$464$(E-Mail Removed)>,
Elegie <(E-Mail Removed)> 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
 
Reply With Quote
 
Tim Streater
Guest
Posts: n/a
 
      03-06-2007
In article <(E-Mail Removed). com>,
"Tom Cole" <(E-Mail Removed)> wrote:

> On Mar 6, 9:25 am, Tim Streater <(E-Mail Removed)> 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
 
Reply With Quote
 
Tim Streater
Guest
Posts: n/a
 
      03-07-2007
In article <45edc6b6$0$28177$(E-Mail Removed)>,
Michael White <(E-Mail Removed)> wrote:

> Tim Streater wrote:
>
> > In article <45ed7e8a$0$464$(E-Mail Removed)>,
> > Elegie <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
char arrays and integer arrays... why the difference? Bill Reyn C++ 3 06-22-2004 12:01 PM
Arrays.asList() returning java.util.Arrays$ArrayList Alexandra Stehman Java 5 06-17-2004 06:04 PM
initializing arrays of arrays Mantorok Redgormor C Programming 4 09-11-2003 02:08 AM



Advertisments