KBD

Keith Devens .com

Saturday, October 11, 2008 Flag waving
I was always told, that if I ain't gonna be part of the greatest, I gotta be the greatest... – Busta Rhymes
← 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


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)
:

October 2008
SunMonTueWedThuFriSat
 1234
567891011
12131415161718
19202122232425
262728293031 



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

Recent comments XML

new⇒I hate PHP

Elliot Anderson,

Dude!! You the​man! The reverse replacement for​array_u...

Alex Ndungu: Oct 11, 1:35am

Call a function from a string in Python

?!code:
some_object.__getattribute​__('method_name')()
?!/code

is​the s...

Patrick Corcoran: Oct 8, 3:53pm

Spider solitaire

I have won 185 games of Spider​Solitaire at the "Difficult" level.​ What is...

75.179.28.113: Oct 8, 12:42pm

Sed one-liners

Hi.

I wanted to let you know​that I wrote an article "Famous Sed​One-Lin...

Peteris Krumins: Oct 8, 3:05am

Timesheet Calculator

Hadn't seen it before now, but my​company already uses a time​tracking prog...

Keith: Oct 7, 10:44am

Girls, please don't get breast implants

Hey everyone, 

I am new to this​blog and I have enjoyed reading all​your...

Sarah.M.: Oct 6, 9:45am

Generated in about 0.189s.

(Used 8 db queries)

mobile phone