aString.split is returning unwanted empty element

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

    aString.split is returning unwanted empty element

    Good morning.

    I want to send split to a String such as " abc def " and receive
    back an Array with the two elements String(abc) and String(def); and I want
    to receive back a zero-length Array if the target is empty or contains only
    white space.

    The following code produces an unwanted empty string as the first element.
    This also occurs when the target is a zero-length String or a String
    composed only of spaces.

    After all morning, I can't find the solution. Thanks for any help, Ron.

    " abc def ".split("\\ s+")
    => Array("", "abc", "def")


    " ".split("\\ s+")
    => Array("")



  • Bryce (Work)

    #2
    Re: aString.split is returning unwanted empty element

    On Fri, 22 Aug 2003 12:55:29 -0400, "Ron Brennan" <rbrennan@magma .ca>
    wrote:
    [color=blue]
    >Good morning.
    >
    >I want to send split to a String such as " abc def " and receive
    >back an Array with the two elements String(abc) and String(def); and I want
    >to receive back a zero-length Array if the target is empty or contains only
    >white space.[/color]

    Have you looked at the StringTokenizer class?

    Comment

    • Ron Brennan

      #3
      Re: aString.split is returning unwanted empty element

      Yes, I've looked at it; but it's been deprecated in favor of split.


      "Bryce (Work)" <spamtrap@berze rker-soft.com> wrote in message
      news:ferckvggnd h7163qd409ald7d jk5mll0gp@4ax.c om...[color=blue]
      > On Fri, 22 Aug 2003 12:55:29 -0400, "Ron Brennan" <rbrennan@magma .ca>
      > wrote:
      >[color=green]
      > >Good morning.
      > >
      > >I want to send split to a String such as " abc def " and receive
      > >back an Array with the two elements String(abc) and String(def); and I[/color][/color]
      want[color=blue][color=green]
      > >to receive back a zero-length Array if the target is empty or contains[/color][/color]
      only[color=blue][color=green]
      > >white space.[/color]
      >
      > Have you looked at the StringTokenizer class?[/color]


      Comment

      • Neomorph

        #4
        Re: aString.split is returning unwanted empty element

        On Fri, 22 Aug 2003 16:43:20 -0400, "Ron Brennan" <rbrennan@magma .ca>
        two-finger typed:
        [color=blue]
        >Yes, I've looked at it; but it's been deprecated in favor of split.[/color]

        Why don't you trim(), before you split() ?[color=blue]
        >
        >
        >"Bryce (Work)" <spamtrap@berze rker-soft.com> wrote in message
        >news:ferckvggn dh7163qd409ald7 djk5mll0gp@4ax. com...[color=green]
        >> On Fri, 22 Aug 2003 12:55:29 -0400, "Ron Brennan" <rbrennan@magma .ca>
        >> wrote:
        >>[color=darkred]
        >> >Good morning.
        >> >
        >> >I want to send split to a String such as " abc def " and receive
        >> >back an Array with the two elements String(abc) and String(def); and I[/color][/color]
        >want[color=green][color=darkred]
        >> >to receive back a zero-length Array if the target is empty or contains[/color][/color]
        >only[color=green][color=darkred]
        >> >white space.[/color]
        >>
        >> Have you looked at the StringTokenizer class?[/color]
        >[/color]

        Cheers.

        Comment

        • Ron Brennan

          #5
          Re: aString.split is returning unwanted empty element


          "Neomorph" <neomorph@nospa m.demon.co.uk> wrote in message
          news:f6pekvs2u5 1h8fce4g59q7sa7 7b9u92kpv@4ax.c om...
          On Fri, 22 Aug 2003 16:43:20 -0400, "Ron Brennan" <rbrennan@magma .ca>
          two-finger typed:
          [color=blue]
          >Yes, I've looked at it; but it's been deprecated in favor of split.[/color]

          Why don't you trim(), before you split() ?[color=blue]
          >
          >
          >"Bryce (Work)" <spamtrap@berze rker-soft.com> wrote in message
          >news:ferckvggn dh7163qd409ald7 djk5mll0gp@4ax. com...[color=green]
          >> On Fri, 22 Aug 2003 12:55:29 -0400, "Ron Brennan" <rbrennan@magma .ca>
          >> wrote:
          >>[color=darkred]
          >> >Good morning.
          >> >
          >> >I want to send split to a String such as " abc def " and[/color][/color][/color]
          receive[color=blue][color=green][color=darkred]
          >> >back an Array with the two elements String(abc) and String(def); and I[/color][/color]
          >want[color=green][color=darkred]
          >> >to receive back a zero-length Array if the target is empty or contains[/color][/color]
          >only[color=green][color=darkred]
          >> >white space.[/color]
          >>
          >> Have you looked at the StringTokenizer class?[/color]
          >[/color]

          Cheers.

          Yes, I did, and it worked.

          Thank you.

          Ron.



          Comment

          • Bryce (Work)

            #6
            Re: aString.split is returning unwanted empty element

            On Mon, 25 Aug 2003 22:09:06 +0200, Neomorph
            <neomorph@nospa m.demon.co.uk> wrote:
            [color=blue]
            >On Mon, 25 Aug 2003 10:54:59 -0400, "Bryce (Work)"
            ><spamtrap@berz erker-soft.com> two-finger typed:
            >[color=green]
            >>On Fri, 22 Aug 2003 16:43:20 -0400, "Ron Brennan" <rbrennan@magma .ca>
            >>wrote:
            >>[color=darkred]
            >>>Yes, I've looked at it; but it's been deprecated in favor of split.[/color]
            >>
            >>Hey... I learned something new today. I hadn't realized it had been
            >>deprecated. ..[/color]
            >
            >StringTokenize r is not listed in the deprecated API for the 1.4.2 version
            >of the JSDK.[/color]

            I know.. But if you read the API for 1.4.2, it says:



            StringTokenizer is a legacy class that is retained for compatibility
            reasons although its use is discouraged in new code. It is recommended
            that anyone seeking this functionality use the split method of String
            or the java.util.regex package instead.

            --- END QUOTE --

            Note, that this does not appear in the 1.4.1 docs. Maybe its just prep
            for them deprecating in 1.5...

            Comment

            Working...