I put this in the category of functions of "so easy to write I don't bother to look for a library implementation". I can probably write this faster than I can do a Google search for an existing implementation, and then if I need some variation, I have the source, I understand it, etc.
In many projects I've created a class like this:
public class Joiner
{
String prefix;
String conj;
String use;
StringBuilder sb;
public Joiner(String prefix, String conj)
{
prefix=this.prefix;
conj=this.conj;
use=prefix;
sb=new StringBuilder();
}
public Joiner(String conj)
{
this("",conj);
}
public Joiner append(String s)
{
sb.append(use).append(s);
use=conj;
}
public boolean isEmpty()
{
return sb.length()==0;
}
public String toString()
{
return sb.toString();
}
}
Note this allows a prefix to be placed in front of the first item and then some other conjunction used after that.
This has many uses. Like, to make a comma-separated list:
Joiner commaList=new Joiner(",");
commaList.append("Item1").append("Item2").append("Item3");
Or, to build a SQL WHERE clause with conditions ANDed together:
Joiner where=new Joiner(" where ", " and ");
... bunch of code to find value for foo ...
where.append("foo="+quote(foo));
... bunch of code to find value for bar ...
where.append("bar="+quote(bar));
... etc ...
(I'm assuming the "quote" function puts quotes around strings and escapes any embedded quotes. You don't stick a user input into a SQL string build without making sure to escape embedded quotes, do you?)
The prefix is only added if we have at least one entry, which works nicely in many contexts where we string this thing in with other stuff.
=====
Edit: Addendum:
Hmm, I've been downvoted at least twice for suggesting that one write simple routines yourself instead of searching the Internet for code written by someone else. How curious. I would think there's an obvious trade-off here.
The obvious advantage of getting some open source library is that someone else has already done all the work and hopefully tested and debugged it.
But open source is not really free. Searching for open source, downloading it, figuring out how to use it, and verifying that it meets your needs surely takes at least a few hours. Depending on the complexity of the requirement, possibly days or weeks. The code may or may not be easy to customize -- and as it's written by someone else, at best you'll have to figure it out. And while one might hope that it has been tested and debugged, it may not be.
If I know that it would take me significantly longer to write some code I need than to find it pre-written, sure I'm going to look for open source or a commercial package. It would take extraordinarily unusual requirements to justify writing my own database engine or inventing a new programming language.
But if I need a modest function to concatenate two strings or whatever, something that I know I can write myself in half an hour and test as part of testing the bigger program, I just do it. I can probably write it myself faster than I could find it written by someone else, and when I do, I know that it will do exactly what I want and not have to worry about undesired behavior in special cases.