StringUtil { public static String[] tokenStringToArray( final String tokenizedString, final String token ) { if ( tokenizedString == null ) { return ( null ); } if ( token == null ) { return ( new String[] { tokenizedString } ); } StringTokenizer st = new StringTokenizer( tokenizedString, token ); List<String> strList = new ArrayList<String>(); while ( st.hasMoreTokens() ) { String tok = st.nextToken(); strList.add( tok ); } String[] rtnArray = new String[ strList.size() ]; return strList.toArray( rtnArray ); } private  StringUtil(); static String[] tokenStringToArray( final String tokenizedString, final String token ); static boolean doesPathContainParentPathSegment( final String path ); static void main( final String[] args ); static boolean isEmpty( final String str ); static String getMapAsPrettyString( final Map map ); static Map<String, String> trimStringMap( Map<String, String> map ); static List<String> trimStringList( List<String> list ); static String trimToEmpty( String string );  }
@Test public void testTokenStringToArray() { String token = " "; String tokenString = "This is a test to convert this string into an array"; String[] array = StringUtil.tokenStringToArray( tokenString, token ); StringBuilder buffer = new StringBuilder(); for ( String element : array ) { buffer.append( element ); buffer.append( token ); } Assert.assertEquals( buffer.toString().trim(), tokenString ); Assert.assertNull( StringUtil.tokenStringToArray( null, token ) ); Assert.assertNull( StringUtil.tokenStringToArray( null, null ) ); tokenString = "some text"; array = StringUtil.tokenStringToArray( tokenString, null ); Assert.assertNotNull( array ); Assert.assertEquals( 1, array.length ); Assert.assertEquals( tokenString, array[ 0 ] ); }