Recently I have been working on a BlackBerry application that required the use of a String split routine. However, for any BlackBerry developers out there you will know that the J2ME packaged with the BlackBerry OS does not contain a String.split() routine. Because if that I have had to write my own String.split() routine. Below is the source code available for the class method.

	/**
	* split
	*
	* Splits a string by the specified delimiter into an array of strings.
	*
	* @access:  public static
	* @param:   String				Contains the string to split.
	* @param:	String				Contains the delimiter.
	* @return:  String[]			Returns a String array of pieces split by the delimiter. May return null in some cases.
	*/
	public static String[] split(String str, String delimiter) {
		String[] stringPieces;
		try {
			Vector v = new Vector();
			int start = 0;
			int end   = str.indexOf(delimiter);

			while( -1 != end ) {
				if( end > start ) {
					v.addElement(new String(str.substring(start, end)));
				}

				start = end + delimiter.length();
				end   = str.indexOf(delimiter, start);
			}
			v.addElement(new String(str.substring(start, str.length())));

			stringPieces = new String[v.size()];
			for( int i = 0; i < v.size(); ++i ) {
				stringPieces[i] = v.elementAt(i).toString();
			}
		}
		catch( Exception e ) {
			System.err.println(e.toString());
			stringPieces = null;
		}

		return stringPieces;
	}

Remember when using the method above, you will require the java.util.Vector package to be imported.

I am sharing this method with you as I believe it will be useful to many of developers out there looking for a simple String split() routine.