Google Guava is a java library with lot of utilities and reusable components. This requires the library guava-10.0.jar to be in classpath. The following example shows using Joiner class. It joins the given strings with provided separator.
package com.bethecoder.tutorials.guava.base_tests; import com.google.common.base.Joiner; public class JoinTest { /** * @param args */ public static void main(String[] args) { Joiner joiner = Joiner.on(":"); String str = joiner.join("One", "Two", "Three", "Four"); //We shouldn't pass null values here //To handle nulls we should either use //skipNulls() or useForNull(String) methods //Throws NullPointerException otherwise System.out.println(str); } }
One:Two:Three:Four