Java 8 – How to join String[] Arrays elements using delimiter ?

In this article, we will understand with a Java program on how to join String[] Arrays elements using different delimiter like dot, hyphen, forward-slash, colon, semi-colon, comma, pipe and space, etc., using Java 1.8 version

Already in one of the previous article, we discussed how to join String[] Arrays elements using different delimiters.

Join String[] Arrays elements using Delimiter :

  • join() method of String
    • This is introduced in Java 1.8 version for joining CharSequence, List of elements or Arrays elements with specified delimiters
  • There are 2 variants of join() method,
    • String join(CharSequence delimiter, CharSequence… elements); – Returns all elements specified in the method-argument joined together with the specified delimiter
    • String join(CharSequence delimiter, Iterable elements); – Returns all elements present in Collection object (or Iterable object) joined together with the specified delimiter
  • In below illustration, we will cover joining Arrays elements using 2nd variant of String.join() method
  • Delimiters used for the below illustration are,
    • Space ( )
    • Dot (.)
    • Hyphen (-)
    • Forward-slash (/)
    • Pipe (|)
    • Comma (,)
    • Colon (:)
    • Semi-colon (;)

StringJoinArraysElements.java

package in.bench.resources.java8.string.methods;

public class StringJoinArraysElements {

	public static void main(String[] args) {

		// Arrays elements
		String[] strArrays = {"Java", "is", "a", "super", "cool", "language"};


		// 1. join Arrays elements with space delimiter ( )
		String joinSpaceDelimiterStr = String.join(" ", strArrays);
		System.out.println("Space delimited string :- " + joinSpaceDelimiterStr);


		// 2. join Arrays elements with Dot delimiter (.)
		String joinDotDelimiterStr = String.join(".", strArrays);
		System.out.println("Dot delimited string :- " + joinDotDelimiterStr);


		// 3. join Arrays elements with Hyphen delimiter (-)
		String joinHypehnDelimiterStr = String.join("-", strArrays);
		System.out.println("Hyphen delimited string :- " + joinHypehnDelimiterStr);


		// 4. join Arrays elements with forward-slash delimiter (/)
		String joinFwdSlashDelimiterStr = String.join("/", strArrays);
		System.out.println("Forward-slash delimited string :- " + joinFwdSlashDelimiterStr);


		// 5. join Arrays elements with pipe delimiter (|)
		String joinPipeDelimiterStr = String.join("|", strArrays);
		System.out.println("Pipe delimited string :- " + joinPipeDelimiterStr);


		// 6. join Arrays elements with comma delimiter (,)
		String joinCommaDelimiterStr = String.join(",", strArrays);
		System.out.println("Comma delimited string :- " + joinCommaDelimiterStr);


		// 7. join Arrays elements with colon delimiter (:)
		String joinColonDelimiterStr = String.join(":", strArrays);
		System.out.println("Colon delimited string :- " + joinColonDelimiterStr);


		// 8. join Arrays elements with semi-colon delimiter (:)
		String joinSemiColonDelimiterStr = String.join(";", strArrays);
		System.out.println("Semi-colon delimited string :- " + joinSemiColonDelimiterStr);
	}
}

Output:

Space delimited string :- Java is a super cool language
Dot delimited string :- Java.is.a.super.cool.language
Hyphen delimited string :- Java-is-a-super-cool-language
Forward-slash delimited string :- Java/is/a/super/cool/language
Pipe delimited string :- Java|is|a|super|cool|language
Comma delimited string :- Java,is,a,super,cool,language
Colon delimited string :- Java:is:a:super:cool:language
Semi-colon delimited string :- Java;is;a;super;cool;language

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 - How to join List of String elements using different delimiter ?
Java 8 - How to convert primitive data-types into String ?