3 Replies Latest reply: Jan 15, 2007 11:55 AM by 796440 RSS

    StringTokenizer

    807607
      I know its not widely used or accepted, the split method of the String class used as an alternative but I have a couple of questions.

      Is it possible to set multiple delimeters?

      If i set a delimeter as " " (1 space) and the data file contained data separated by 2 or more spaces is it the same?
        • 1. Re: StringTokenizer
          800322
          Is it possible to set multiple delimeters?
          Yes. Every char in the delimiter String is a delimter, so you can have as many as you like. If you think about multi-char delimiters: no.
          If i set a delimeter as " " (1 space) and the data
          file contained data separated by 2 or more spaces is
          it the same?
          No, each space will be a splitting point. (I think. It's been a while. It'd be easy to write test code, you know.)
          • 2. Re: StringTokenizer
            796440
            This should all be covered by the docs, and any lingering doubts can be answered with a little bit of test code.

            http://java.sun.com/j2se/1.5.0/docs/api/java/util/StringTokenizer.html
            • 3. Re: StringTokenizer
              807607
              No, each space will be a splitting point. (I think. It's
              been a while. It'd be easy to write test code, you know.)
              No, it's the other way 'round: two or more spaces will always be treated as a single delimiter by StringTokenizer. To get the same behavior from split() you have to add a plus sign:
                String[] tokens = str.split(" +");