When it comes to string manipulation, it is not uncommon to want to split a single string into multiple strings based on a delimiter. Many languages provide split functionality outright. Even in C, it's fairly easy to roll your own --- assuming you don't like strtok_r --- with functions like strchr or strpbrk.

Jake says that, in Java, this was not the case until 1.4, which is the same thing the documentation says. Apparently one of the developers with whom he works still does not realize this. The following function was developed recently and is still being used in new code.

   public void setXXXXXXXX(String str)
   {
       log.debug("in XXXXXX, str = " + str);
       if (str == null)
       {
           str = "";
       }
       StringTokenizer st = new StringTokenizer(str, ";");
       Vector v = new Vector(st.countTokens());
       while (st.hasMoreTokens())
       {
           String tok = st.nextToken();
           if (tok != null)
           {
               tok = tok.trim();
               if (tok.length() > 0)
               {                  v.addElement(tok);
               }
           }
  }
       bureauLocations = new String[v.size()];
       int i = 0;
   for (Enumeration e = v.elements(); e.hasMoreElements(); )
       {
       bureauLocations[i++] = (String)e.nextElement();
       }
   }
Just to rub it in Java's face, Pete shares how easy it is to use split in Perl.
sub split_at_tabs {
   my ($line) = @_;
   my @_record = split(/(\t)/,$line."\t");
   my @record = map { $_ eq "\t" ? () : $_ } @_record;
   return \@record;
}

(He counts four things wrong with that function. Can you find them all?)
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!