Create array from string with different delimiters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aliensite

    #1

    Create array from string with different delimiters

    How to create an array if the user enters any of the following formats:
    1 2 3 4 5.0
    1,2,3,4,5.0
    1, 2, 3, 4, 5.0

    and if possible
    1,2 ,3, 4 5.0
  • Dr John Stockton

    #2
    Re: Create array from string with different delimiters

    JRS: In article <ca0d9b0f.04021 61154.3d4473e4@ posting.google. com>, seen
    in news:comp.lang. javascript, aliensite <aliensite@exci te.com> posted at
    Mon, 16 Feb 2004 11:54:46 :-[color=blue]
    >How to create an array if the user enters any of the following formats:
    >1 2 3 4 5.0
    >1,2,3,4,5.0
    >1, 2, 3, 4, 5.0
    >
    >and if possible
    >1,2 ,3, 4 5.0[/color]

    var S = <as above>

    A = S.split(/[^0-9\.]/)

    where the contents of the square brackets represent all characters that
    may be present between numbers. You may want to exclude from that plus,
    minus, and possibly E and/or X.

    Note that you will have an array of Strings

    for (j=0;j<A.length ;j++) A[j] = + A[j] // for Numbers

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
    <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
    <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

    Comment

    • Brian Genisio

      #3
      Re: Create array from string with different delimiters

      aliensite wrote:
      [color=blue]
      > How to create an array if the user enters any of the following formats:
      > 1 2 3 4 5.0
      > 1,2,3,4,5.0
      > 1, 2, 3, 4, 5.0
      >
      > and if possible
      > 1,2 ,3, 4 5.0[/color]


      Try this

      // Testing string
      var text = "1,2 ,3, 4 5.0";

      // replace all commas with spaces
      text = text.replace(/[,]/g, " ");
      // replace all space strings of 2 or more with a single space
      text = text.replace(/[ ][ ]+/g, " ");
      // split into an array based off of spaces
      values = text.split(" ");

      // test it out
      for( i in values )
      alert(values[i]);

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: Create array from string with different delimiters

        Dr John Stockton <spam@merlyn.de mon.co.uk> writes:
        [color=blue]
        > JRS: In article <ca0d9b0f.04021 61154.3d4473e4@ posting.google. com>, seen
        > in news:comp.lang. javascript, aliensite <aliensite@exci te.com> posted at
        > Mon, 16 Feb 2004 11:54:46 :-[/color]
        [color=blue][color=green]
        >>1,2 ,3, 4 5.0[/color][/color]
        [color=blue]
        > A = S.split(/[^0-9\.]/)[/color]

        I think you forgot the +, i.e.,
        A = S.split(/[^0-9\.]+/)
        Otherwise "3, 4" will be split into three parts, the middle one empty.

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • aliensite

          #5
          Re: Create array from string with different delimiters

          Brian Genisio <BrianGenisio@y ahoo.com> wrote in message news:<40321139@ 10.10.0.241>...[color=blue]
          > aliensite wrote:
          >[color=green]
          > > How to create an array if the user enters any of the following formats:
          > > 1 2 3 4 5.0
          > > 1,2,3,4,5.0
          > > 1, 2, 3, 4, 5.0
          > >
          > > and if possible
          > > 1,2 ,3, 4 5.0[/color]
          >
          >
          > Try this
          >
          > // Testing string
          > var text = "1,2 ,3, 4 5.0";
          >
          > // replace all commas with spaces
          > text = text.replace(/[,]/g, " ");
          > // replace all space strings of 2 or more with a single space
          > text = text.replace(/[ ][ ]+/g, " ");
          > // split into an array based off of spaces
          > values = text.split(" ");
          >
          > // test it out
          > for( i in values )
          > alert(values[i]);[/color]

          Thanks!
          I also found this link helpfull:
          http://tinyurl.com/366sy
          at www.breakingpar.com.

          Comment

          • Dr John Stockton

            #6
            Re: Create array from string with different delimiters

            JRS: In article <d68dqq2t.fsf@h otpop.com>, seen in
            news:comp.lang. javascript, Lasse Reichstein Nielsen <lrn@hotpop.com >
            posted at Tue, 17 Feb 2004 23:49:30 :-[color=blue]
            >Dr John Stockton <spam@merlyn.de mon.co.uk> writes:
            >[color=green]
            >> JRS: In article <ca0d9b0f.04021 61154.3d4473e4@ posting.google. com>, seen
            >> in news:comp.lang. javascript, aliensite <aliensite@exci te.com> posted at
            >> Mon, 16 Feb 2004 11:54:46 :-[/color]
            >[color=green][color=darkred]
            >>>1,2 ,3, 4 5.0[/color][/color]
            >[color=green]
            >> A = S.split(/[^0-9\.]/)[/color]
            >
            >I think you forgot the +, i.e.,
            > A = S.split(/[^0-9\.]+/)
            >Otherwise "3, 4" will be split into three parts, the middle one empty.[/color]


            I tested it, without +, on the OP's strings, and noticed no empty
            element.

            OTOH you are obviously right.

            OTTH, in my MSIE4,
            A = "3, 4".split(/[^0-9\.]/)
            returns [3,4] of .length 2.




            A saved Netscape page implies that .split was different (from what?) in
            JS1.2 -

            <QUOTE>
            Example 2. Consider the following script:

            <SCRIPT LANGUAGE="JavaS cript1.2">
            str="She sells seashells \nby the\n seashore"
            document.write( str + "<BR>")
            a=str.split(" ")
            document.write( a)
            </SCRIPT>

            Using LANGUAGE="JavaS cript1.2", this script produces

            "She", "sells", "seashells" , "by", "the", "seashore"

            Without LANGUAGE="JavaS cript1.2", this script splits only on single
            space characters, producing

            "She", "sells", , , , "seashells" , "by", , , "the", "seashore"

            </QUOTE>



            ECMA 262 explains .split in impenetrable detail.


            OP: Use the +.

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

            Comment

            Working...