KBD

Keith Devens .com

Thursday, March 18, 2010 Flag waving
real software ships. – John Gruber
← Join Java, concurrency extensions to JavaSitePoint Forums - What happened to Object Aggregation? (in PHP) →

Daily link icon Wednesday, August 17, 2005

Java join function

Because Java is too much of a pain in the ass to have included one in the standard library, here's a simple string join function:

public static String join(String[] pieces){
    return join(pieces, " ");
}
public static String join(String[] pieces, char sep){
    return join(pieces, String.valueOf(sep));
}
public static String join(String[] pieces, String sep){
    if(pieces.length == 0) return "";

    StringBuffer buf = new StringBuffer();
    buf.append(pieces[0]);
    for(int i=1,n=pieces.length; i<n; i++)
        buf.append(sep).append(pieces[i]);
    return buf.toString();
}

Update: Of course, there's also a join function in org.apache.commons.lang.StringUtils.

← Join Java, concurrency extensions to JavaSitePoint Forums - What happened to Object Aggregation? (in PHP) →

Comments XML gif

208.28.3.125 wrote:

The code will break if the first value of the array is null.
~Ajay

∴ 208.28.3.125 | 19-Nov-2008 6:19pm est | #10952

Keith (http://keithdevens.com/) wrote:

The code will break if the first value of the array is null.

Have you run it? It should just append the string "null". And why only if the first value is null? The code calls StringBuffer.append for all elements, not just the first.

Keith | 19-Nov-2008 6:35pm est | http://keithdevens.com/ | #10953

208.28.3.125 wrote:

Hi Keith,
My expected behaviour of the method, which I feel is reasonable - when you encounter a null return empty string.

I saw your comment on http://piehead.livejournal.com/268351.html . Your suggestion was to avoid the flag for the first entry.

if we were to modify your method to behave similar to one I just mentioned, using a flag is easiest way to do it. Please let me know what you think about that?

~Ajay

∴ 208.28.3.125 | 19-Nov-2008 7:02pm est | #10954

Keith (http://keithdevens.com/) wrote:

Meh, don't have null strings in your string arrays imo, but you're welcome to modify the above function to do what you'd like.

Keith | 19-Nov-2008 7:51pm est | http://keithdevens.com/ | #10955

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

March 2010
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
28293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 2 posts

Recent comments XML

I hate ASP.NET

I hate ASP... I was doing wonders​with PHP, then suddenly one of my​clients...

Johnies: Mar 17, 6:14am

Quantum physics and free will

I knew you were going to say that....

Tom Massey: Mar 15, 9:26pm

Generated in about 0.107s.

(Used 8 db queries)