Loading ...
Sorry, an error occurred while loading the content.

Re: [JavaScript] String to Array, Array to String, String manipulation

Expand Messages
  • David Smart
    An empty string - split ( ) - works fine in IE6 to split the way you want. Using join ( ) will put it back together fine, as you d expect it to. (I m just
    Message 1 of 6 , Aug 28, 2004
    • 0 Attachment
      An empty string - split ("") - works fine in IE6 to split the way you want.

      Using join ("") will put it back together fine, as you'd expect it to.

      (I'm just heading out the door to the shops - it's daytime here - so I'll look at your other couple of questions when I get back, if someone else doesn't answer. However, have a look a "splice" and ? "slice" for your string manipulations, they may be what you want.

      JavaScript is only just OK for string handling. I extend the String class with other useful (to me) things so that I get pretty good string handling.

      Dave S

      ----- Original Message -----
      From: cfx11
      To: javascript_official@yahoogroups.com
      Sent: Sunday, August 29, 2004 8:59 AM
      Subject: [JavaScript] String to Array, Array to String, String manipulation


      Howdy everyone,

      I was wondering, what can I do to conver a string into an array
      and viceversa?
      For instance, if I had
      x = "abcdefg"

      and I would like an array, each one of one letter
      x = new Array('a','b','c','d','e','f','g')

      I know about a string operator that is split(); however, I think
      that split() requires a character marker... if everything is
      sequential, what can the marker be?

      The second thing that I was wondering about is how can I
      manipulate strings
      For instance, if I had the following strings
      x="bc"
      y="ad"

      and I would like to insert "bc" between ad... how can I manipulate
      it so that I can do that so that I can get
      z="abcd"

      Lastly, I would like to know how good is JavaScript for string
      handling? Is there any good reference or cookbook for this kind of
      thing. The reason I am using JavaScript for my project is because it
      has very nice features to format the text and making things more
      visual; however, I also needed the power of programming language.


      Thank you!



      Yahoo! Groups Sponsor
      ADVERTISEMENT





      ------------------------------------------------------------------------------
      Yahoo! Groups Links

      a.. To visit your group on the web, go to:
      http://groups.yahoo.com/group/javascript_official/

      b.. To unsubscribe from this group, send an email to:
      javascript_official-unsubscribe@yahoogroups.com

      c.. Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.



      ---
      Outgoing mail is certified Virus Free.
      Checked by AVG anti-virus system (http://www.grisoft.com).
      Version: 6.0.744 / Virus Database: 496 - Release Date: 24/08/04

      [Non-text portions of this message have been removed]
    • David Smart
      Further to my previous - I don t think splice is what you want, sorry. That s an array operator only. Your easiest way to do it is from first principals -
      Message 2 of 6 , Aug 28, 2004
      • 0 Attachment
        Further to my previous - I don't think "splice" is what you want, sorry. That's an array operator only.

        Your easiest way to do it is from first principals - i.e. using substr to split the string and the + operator to put it all back together again.

        This link http://www.iota-six.co.uk/javascript/08_strin.htm is into the middle of what looks like quite a good JavaScript introduction. Certainly, the section on strings discusses everything you've brought up (including the split ("") to split the characters up).

        Dave S

        ----- Original Message -----
        From: cfx11
        To: javascript_official@yahoogroups.com
        Sent: Sunday, August 29, 2004 8:59 AM
        Subject: [JavaScript] String to Array, Array to String, String manipulation


        Howdy everyone,

        I was wondering, what can I do to conver a string into an array
        and viceversa?
        For instance, if I had
        x = "abcdefg"

        and I would like an array, each one of one letter
        x = new Array('a','b','c','d','e','f','g')

        I know about a string operator that is split(); however, I think
        that split() requires a character marker... if everything is
        sequential, what can the marker be?

        The second thing that I was wondering about is how can I
        manipulate strings
        For instance, if I had the following strings
        x="bc"
        y="ad"

        and I would like to insert "bc" between ad... how can I manipulate
        it so that I can do that so that I can get
        z="abcd"

        Lastly, I would like to know how good is JavaScript for string
        handling? Is there any good reference or cookbook for this kind of
        thing. The reason I am using JavaScript for my project is because it
        has very nice features to format the text and making things more
        visual; however, I also needed the power of programming language.


        Thank you!



        Yahoo! Groups Sponsor
        ADVERTISEMENT





        ------------------------------------------------------------------------------
        Yahoo! Groups Links

        a.. To visit your group on the web, go to:
        http://groups.yahoo.com/group/javascript_official/

        b.. To unsubscribe from this group, send an email to:
        javascript_official-unsubscribe@yahoogroups.com

        c.. Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.



        ---
        Outgoing mail is certified Virus Free.
        Checked by AVG anti-virus system (http://www.grisoft.com).
        Version: 6.0.744 / Virus Database: 496 - Release Date: 24/08/04

        [Non-text portions of this message have been removed]
      • David Smart
        Kevin Can t say I agree with you. JavaScript is more than a simple scripting language. In particular, it provides string handling that doesn t seem any less
        Message 3 of 6 , Aug 30, 2004
        • 0 Attachment
          Kevin

          Can't say I agree with you.

          JavaScript is more than a simple scripting language. In particular, it provides string handling that doesn't seem any less capable than that provided by Java.

          The big benefit of JavaScript, of course, is that you can freely intersperse your JavaScript code throughout your HTML - meaning that you don't have to do as much coding to get a page out. However, don't confuse this flexibility with lack of functionality.

          JavaScript provides object-based programming, allows extensions to standard object types, and allows for the creation of user objects - things one wouldn't expect of a simple scripting language.

          Dave S

          ----- Original Message -----
          From: Kevin Stewart
          To: javascript_official@yahoogroups.com
          Sent: Sunday, August 29, 2004 10:43 AM
          Subject: Re: [JavaScript] String to Array, Array to String, String manipulation


          I'm curious to see the answers to your questions too. But
          I wondered if Java would be a better choice for you. You
          said you wanted a programming language and Javascript
          really is just a scripting language. But I do think you
          can integrate Java in to do the hard work for you. My rule
          of thumb is to let each language do what it does best -
          HTML for content, CSS for layout, Javascript for simple
          events and Java for - programming. Just my $0.02
          worth.

          Kevin


          --- cfx11 <no_reply@yahoogroups.com> wrote:

          > Howdy everyone,
          >
          > I was wondering, what can I do to conver a string into
          > an array
          > and viceversa?
          > For instance, if I had
          > x = "abcdefg"
          >
          > and I would like an array, each one of one letter
          > x = new Array('a','b','c','d','e','f','g')
          >
          > I know about a string operator that is split();
          > however, I think
          > that split() requires a character marker... if everything
          > is
          > sequential, what can the marker be?
          >
          > The second thing that I was wondering about is how can
          > I
          > manipulate strings
          > For instance, if I had the following strings
          > x="bc"
          > y="ad"
          >
          > and I would like to insert "bc" between ad... how can I
          > manipulate
          > it so that I can do that so that I can get
          > z="abcd"
          >
          > Lastly, I would like to know how good is JavaScript for
          > string
          > handling? Is there any good reference or cookbook for
          > this kind of
          > thing. The reason I am using JavaScript for my project is
          > because it
          > has very nice features to format the text and making
          > things more
          > visual; however, I also needed the power of programming
          > language.
          >
          >
          > Thank you!
          >


          ---
          Outgoing mail is certified Virus Free.
          Checked by AVG anti-virus system (http://www.grisoft.com).
          Version: 6.0.745 / Virus Database: 497 - Release Date: 27/08/04

          [Non-text portions of this message have been removed]
        • Kevin Stewart
          Dave, I have to admit I don t know Javascript that well yet so my opinion isn t worth all that much. But I do think it is MUCH less functional than Java. I m
          Message 4 of 6 , Aug 30, 2004
          • 0 Attachment
            Dave,

            I have to admit I don't know Javascript that well yet so my
            opinion isn't worth all that much. But I do think it is
            MUCH less functional than Java. I'm a professional Java
            programmer so I thought coming into Javascript would be
            easy. But every where I go it get "ya, you can do that in
            Java, but Javascript isn't Java so you can't do that with
            it" type comments. I haven't gotten into extending objects
            in Javascript yet (looking for a good tutorial on it
            acutally) so maybe I'm missing that extra piece. But
            String manipulations like the original poster mentioned
            would be kids play in Java, no extensions required.

            I never said that Javascript was a SIMPLE scripting
            language, just that it was a scripting language (hence the
            name) and not a full programming language. Every book and
            tutorial I've seen says the same thing up front. But as
            you say, it is easily embedded into HTML so it has many
            advantages that Java does not, but Java has many that
            Javascript does not. So why should people kill themselves
            finding ways to do things in Javascript that are easy in
            Java?

            My point was and is that we should use Javascript for what
            it is good at and use Java for what it is good at. People
            can use screwdrivers as hammers, but isn't it better to use
            them as screwdrivers and let the hammers be hammers? The
            trick is, of course, to know where the line is and that is
            part of what I'm here to learn.

            Kevin


            --- David Smart <smartware@...> wrote:

            > Kevin
            >
            > Can't say I agree with you.
            >
            > JavaScript is more than a simple scripting language. In
            > particular, it provides string handling that doesn't seem
            > any less capable than that provided by Java.
            >
            > The big benefit of JavaScript, of course, is that you can
            > freely intersperse your JavaScript code throughout your
            > HTML - meaning that you don't have to do as much coding
            > to get a page out. However, don't confuse this
            > flexibility with lack of functionality.
            >
            > JavaScript provides object-based programming, allows
            > extensions to standard object types, and allows for the
            > creation of user objects - things one wouldn't expect of
            > a simple scripting language.
            >
            > Dave S
            >



            __________________________________
            Do you Yahoo!?
            Take Yahoo! Mail with you! Get it on your mobile phone.
            http://mobile.yahoo.com/maildemo
          • David Smart
            Kevin My feeling is that the word script in JavaScript, JScript and VBScript is slightly misused and that it very definitely serves as a put down for what
            Message 5 of 6 , Aug 30, 2004
            • 0 Attachment
              Kevin

              My feeling is that the word "script" in JavaScript, JScript and VBScript is slightly misused and that it very definitely serves as a "put down" for what are actually fairly complete languages.

              My feeling for what constitutes a scripting language is something akin to the scripters in Unix that provide a level of programming extensions to the command language, and are closely integrated with that language.

              I think the term "script" was used because these are interpreted (rather than compiled) languages. Certainly the more precise name for JavaScript - "InterpretedLanguageLooselyBasedOnJava" - lacks a certain punch. :-)

              JavaScript, etc, are actually quite remote from the HTML they sit in. They don't provide a direct method for generating HTML - you have to format it up and write it out with a "write" statement. They don't provide integrated (language) access to the browser page - you use somewhat inconvenient references to objects in the browser.

              They do, however, all provide a pretty complete programming environment.

              You say in part "... So why should people kill themselves finding ways to do things in JavaScript that are easy in Java? ..." A perfectly valid point, but I'd also want to reverse it. It is by no means trivial to include small chunks of Java code in web pages. My feeling is that you'd only want to resort to Java when you had a significant item to program or when JavaScript clearly was lacking in facilities.

              I'd be interested to hear some of the "can do in Java, can't do in JavaScript" type comments. My guess is that the people who say this may not know JavaScript at more than a basic level. In general, you can do it with JavaScript. On occasions, of course, it will be much easier to do it with Java.

              Going back to the string manipulation of the original question (working from memory, don't have the original message in front of me) ...

              Break a string character by character into an array:

              var myString = "abcdefg";
              var myArray = myString.split ("");

              Put it back together again:

              var myNewString = myArray.join ("");

              Java very similar - use "toCharArray" to break it and probably "toString" on the array to put it back.

              var string1 = "ad";
              var string2 = "bc";
              // desired result "abcd";

              var result = string1.charAt (0) + string2 + string1.charAt (1);

              Java also has charAt and the statement would be similar.

              If bigger chinks were needed, substr or substring would be used instead of charAt. Or (shudder) use a string buffer in Java.

              Summary ... definitely horses for courses, but IMHO most browser page manipulations are easier to implement in JavaScript than in Java. On the other hand, of course, JavaScript isn't useful for anything else, but Java certainly is. Where should the line be drawn between them - dunno for sure, and it certainly depends on the programmer's comfort level with the two languages.

              Dave S

              ----- Original Message -----
              From: Kevin Stewart
              To: javascript_official@yahoogroups.com
              Sent: Monday, August 30, 2004 10:21 PM
              Subject: Re: [JavaScript] String to Array, Array to String, String manipulation


              Dave,

              I have to admit I don't know Javascript that well yet so my
              opinion isn't worth all that much. But I do think it is
              MUCH less functional than Java. I'm a professional Java
              programmer so I thought coming into Javascript would be
              easy. But every where I go it get "ya, you can do that in
              Java, but Javascript isn't Java so you can't do that with
              it" type comments. I haven't gotten into extending objects
              in Javascript yet (looking for a good tutorial on it
              acutally) so maybe I'm missing that extra piece. But
              String manipulations like the original poster mentioned
              would be kids play in Java, no extensions required.

              I never said that Javascript was a SIMPLE scripting
              language, just that it was a scripting language (hence the
              name) and not a full programming language. Every book and
              tutorial I've seen says the same thing up front. But as
              you say, it is easily embedded into HTML so it has many
              advantages that Java does not, but Java has many that
              Javascript does not. So why should people kill themselves
              finding ways to do things in Javascript that are easy in
              Java?

              My point was and is that we should use Javascript for what
              it is good at and use Java for what it is good at. People
              can use screwdrivers as hammers, but isn't it better to use
              them as screwdrivers and let the hammers be hammers? The
              trick is, of course, to know where the line is and that is
              part of what I'm here to learn.

              Kevin


              ---
              Outgoing mail is certified Virus Free.
              Checked by AVG anti-virus system (http://www.grisoft.com).
              Version: 6.0.745 / Virus Database: 497 - Release Date: 27/08/04

              [Non-text portions of this message have been removed]
            • Kevin Stewart
              David, I think we re fairly close to a meeting of the minds here. The main difference is where the line should be drawn on what is best done in Java verses
              Message 6 of 6 , Sep 1, 2004
              • 0 Attachment
                David,

                I think we're fairly close to a meeting of the minds here.
                The main difference is where the line should be drawn on
                what is best done in Java verses Javascript. And being new
                to Javascript I have to say that I really don't know, yet.

                As for the term 'script' I really don't use that as a
                negative. It does imply some limitations, but at the same
                time it implies some strengths. More of a "different but
                equal" sorta thing in my mind. I'm sorry if you thought I
                was speaking negatively about Javascript. (BTW I'd still
                like to see a good tutorial on the OO aspects of Javascript
                if you have one.)

                As for the "can do in Java, can't do in JavaScript", that's
                at the start of every tutorial I've seen. From what I've
                seen JS has most of the operators that Java has, what I
                haven't seen yet is the built in classes. Java has built
                in classes to do image manipulation, GUI building, advanced
                string parsing and formatting, variable storage (i.e.
                Vector) and a few others. And of classes like String that
                JS does have, Java has more built-in functions. Likely you
                could make this in JS, but they're already there in Java.
                I think that's the kind of thing they're talking about. At
                least that's what I've seen most noticably.

                Kevin


                --- David Smart <smartware@...> wrote:

                > Kevin
                >
                > My feeling is that the word "script" in JavaScript,
                > JScript and VBScript is slightly misused and that it very
                > definitely serves as a "put down" for what are actually
                > fairly complete languages.
                >
                > My feeling for what constitutes a scripting language is
                > something akin to the scripters in Unix that provide a
                > level of programming extensions to the command language,
                > and are closely integrated with that language.
                >
                > I think the term "script" was used because these are
                > interpreted (rather than compiled) languages. Certainly
                > the more precise name for JavaScript -
                > "InterpretedLanguageLooselyBasedOnJava" - lacks a certain
                > punch. :-)
                >
                > JavaScript, etc, are actually quite remote from the HTML
                > they sit in. They don't provide a direct method for
                > generating HTML - you have to format it up and write it
                > out with a "write" statement. They don't provide
                > integrated (language) access to the browser page - you
                > use somewhat inconvenient references to objects in the
                > browser.
                >
                > They do, however, all provide a pretty complete
                > programming environment.
                >
                > You say in part "... So why should people kill themselves
                > finding ways to do things in JavaScript that are easy in
                > Java? ..." A perfectly valid point, but I'd also want to
                > reverse it. It is by no means trivial to include small
                > chunks of Java code in web pages. My feeling is that
                > you'd only want to resort to Java when you had a
                > significant item to program or when JavaScript clearly
                > was lacking in facilities.
                >
                > I'd be interested to hear some of the "can do in Java,
                > can't do in JavaScript" type comments. My guess is that
                > the people who say this may not know JavaScript at more
                > than a basic level. In general, you can do it with
                > JavaScript. On occasions, of course, it will be much
                > easier to do it with Java.
                >
                > Going back to the string manipulation of the original
                > question (working from memory, don't have the original
                > message in front of me) ...
                >
                > Break a string character by character into an array:
                >
                > var myString = "abcdefg";
                > var myArray = myString.split ("");
                >
                > Put it back together again:
                >
                > var myNewString = myArray.join ("");
                >
                > Java very similar - use "toCharArray" to break it and
                > probably "toString" on the array to put it back.
                >
                > var string1 = "ad";
                > var string2 = "bc";
                > // desired result "abcd";
                >
                > var result = string1.charAt (0) + string2 +
                > string1.charAt (1);
                >
                > Java also has charAt and the statement would be similar.
                >
                > If bigger chinks were needed, substr or substring would
                > be used instead of charAt. Or (shudder) use a string
                > buffer in Java.
                >
                > Summary ... definitely horses for courses, but IMHO most
                > browser page manipulations are easier to implement in
                > JavaScript than in Java. On the other hand, of course,
                > JavaScript isn't useful for anything else, but Java
                > certainly is. Where should the line be drawn between
                > them - dunno for sure, and it certainly depends on the
                > programmer's comfort level with the two languages.
                >
                > Dave S
                >
                > ----- Original Message -----
                > From: Kevin Stewart
                > To: javascript_official@yahoogroups.com
                > Sent: Monday, August 30, 2004 10:21 PM
                > Subject: Re: [JavaScript] String to Array, Array to
                > String, String manipulation
                >
                >
                > Dave,
                >
                > I have to admit I don't know Javascript that well yet
                > so my
                > opinion isn't worth all that much. But I do think it
                > is
                > MUCH less functional than Java. I'm a professional
                > Java
                > programmer so I thought coming into Javascript would be
                > easy. But every where I go it get "ya, you can do that
                > in
                > Java, but Javascript isn't Java so you can't do that
                > with
                > it" type comments. I haven't gotten into extending
                > objects
                > in Javascript yet (looking for a good tutorial on it
                > acutally) so maybe I'm missing that extra piece. But
                > String manipulations like the original poster mentioned
                > would be kids play in Java, no extensions required.
                >
                > I never said that Javascript was a SIMPLE scripting
                > language, just that it was a scripting language (hence
                > the
                > name) and not a full programming language. Every book
                > and
                > tutorial I've seen says the same thing up front. But
                > as
                > you say, it is easily embedded into HTML so it has many
                > advantages that Java does not, but Java has many that
                > Javascript does not. So why should people kill
                > themselves
                > finding ways to do things in Javascript that are easy
                > in
                > Java?
                >
                > My point was and is that we should use Javascript for
                > what
                > it is good at and use Java for what it is good at.
                > People
                > can use screwdrivers as hammers, but isn't it better to
                > use
                > them as screwdrivers and let the hammers be hammers?
                > The
                > trick is, of course, to know where the line is and that
                > is
                > part of what I'm here to learn.
                >
                > Kevin
                >
                >
                > ---
                > Outgoing mail is certified Virus Free.
                > Checked by AVG anti-virus system
                > (http://www.grisoft.com).
                > Version: 6.0.745 / Virus Database: 497 - Release Date:
                > 27/08/04
                >
                > [Non-text portions of this message have been removed]
                >
                >




                _______________________________
                Do you Yahoo!?
                Win 1 of 4,000 free domain names from Yahoo! Enter now.
                http://promotions.yahoo.com/goldrush
              Your message has been successfully submitted and would be delivered to recipients shortly.