2007/05/30

java join and split

I did implement it in my many projects. Because of my common library is not a part of the java standard, I have to write it each time. I think many people must have almost the same experience.


public static String join( String token, String[] strings )
{
StringBuffer sb = new StringBuffer();

for( int x = 0; x < ( strings.length - 1 ); x++ )
{
sb.append( strings[x] );
sb.append( token );
}
sb.append( strings[ strings.length - 1 ] );

return( sb.toString() );
}

for guys who have an old JDK, the split function doesn't exist prior to jdk1.3. The alternative is the following, using the StringTokenizer class

instead of

String line_fields[]=line_data.split("\\t");

use

StringTokenizer line_fields = new StringTokenizer(line_data,"\t");


reference:
the enhancement require to sun
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5015163

No comments: