StringTokenizer question

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

    StringTokenizer question

    Hi,

    I'm having a little problem with the string tokenizer. For example, I got
    this data string :
    "0#0#0#0####0#0 #0#.. and so on....."
    How obvious, the # char will be the delimiter. But as you can see, at some
    points there are two or more brackets pasted together. I want to let the
    program return an empty string but the tokenizer just passes these brackets.
    How to tell him he got to give the empty places as well?

    Greetings,
    Rick


  • Bill Dennis

    #2
    Re: StringTokenizer question

    One of the constructors for StringTokenizer has a boolean indicator
    that tells it to return the delimiters as well. You could use that
    contructor, then in your code loop, you can discard the delimiters,
    but keep a flag so you can catch the condition where you get two
    tokens in a row. In that case, you can process an empty string.

    Bill Dennis

    Comment

    • FISH

      #3
      Re: StringTokenizer question

      "Rick" <aso3rick@hotma il.com> wrote in message news:<3fa77292$ 0$58716$e4fe514 [email protected] l>...[color=blue]
      > Hi,
      >
      > I'm having a little problem with the string tokenizer. For example, I got
      > this data string :
      > "0#0#0#0####0#0 #0#.. and so on....."
      > How obvious, the # char will be the delimiter. But as you can see, at some
      > points there are two or more brackets pasted together. I want to let the
      > program return an empty string but the tokenizer just passes these brackets.
      > How to tell him he got to give the empty places as well?
      >
      > Greetings,
      > Rick[/color]


      If you check in the documentation IIRC you'll find an extra parameter
      which can be used to tell StringTokenizer to return the delimiters as
      well as the tokens. Using this, plus a little extra work, it is
      possible to figure out whether two delimiters have been passed in a
      row, without any token in between, and treat this case as an empty
      token.

      One final thing to note is an issue if the string ends with a delimiter,
      make sure you handle the implied empty token which 'follows' it. You
      could do this by always appending a '#' (the delimiter) onto the end of
      the tokenized string, so any implied empty token at the end of the string
      could be processed without need to write special case code to detect it.


      -FISH- ><>

      Comment

      Working...