Skip to main content

Join or Concatenate Strings with Comma in Java

Banner java icon

🀹 Java String Joining Fun! πŸŽ­β€‹

Hey there, Java ninjas! πŸ₯‹ Today, we’re diving into the world of string concatenation. But waitβ€”this isn't your boring old textbook tutorial. We’re going to make string joining as exciting as assembling your dream burger! πŸ”

Ever found yourself needing to glue together an array of strings, especially when dealing with JSON, XML, or just some fancy output formatting? Well, sit tight because we’ve got you covered! πŸš€


🎯 1. Java 8 – Using String.join()​

Java 8 brought us some cool tools, and String.join() is one of them! Think of it as the duct tape for your strings. πŸ› οΈ

πŸ”Ή 1.1. Join String Literals​

If your strings are all over the place, this method lets you tie them together in one neat package.

String joinedString = String.join(",", "How", "To", "Do", "In", "Java");  
System.out.println(joinedString); // How,To,Do,In,Java

✨ Boom! Instant list without even using an array.

πŸ”Ή 1.2. Joining an Array or List​

Got a string herd (a.k.a. an array)? Round them up into one!

String[] strArray = { "How", "To", "Do", "In", "Java" };
String joinedString = String.join(",", strArray);
System.out.println(joinedString); // How,To,Do,In,Java

🎨 2. Java 8 – StringJoiner for Fancy Output​

If you like your output well-dressed, StringJoiner is here to give it some pizzazz. ✨

πŸ”Ή 2.1. Syntax Magic​

Think of StringJoiner as a personal stylist for your stringsβ€”adding delimiters, prefixes, and suffixes.

StringJoiner joiner = new StringJoiner("," ,"[", "]");
String joinedString = joiner.add("How")
.add("To")
.add("Do")
.add("In")
.add("Java")
.toString();
System.out.println(joinedString); // [How,To,Do,In,Java]

πŸš€ 3. Java 8 – Collectors.joining() for Stream Lovers​

Are you into functional programming and streams? Well, Java has got your back! πŸ’ͺ

List<String> tokens = Arrays.asList("How", "To", "Do", "In", "Java");
String joinedString = tokens.stream()
.collect(Collectors.joining(",", "[", "]"));
System.out.println(joinedString); // [How,To,Do,In,Java]

πŸ’‘ Perfect for handling lists in a breeze!


πŸ› οΈ 4. Apache Commons – StringUtils.join()​

If you’re a power user, the Apache Commons library has some next-level string manipulation tools. πŸ¦Έβ€β™‚οΈ

First, add this to your Maven dependencies:

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>

πŸ”Ή 4.1. Joining Like a Pro​

String[] strArray = { "How", "To", "Do", "In", "Java" };
String joinedString = StringUtils.join(strArray);
System.out.println(joinedString); // HowToDoInJava

String joinedString2 = StringUtils.join(strArray, ",");
System.out.println(joinedString2); // How,To,Do,In,Java

πŸš€ Super fast, super easy!


πŸŽ‰ Wrapping Up​

No matter how you like your string sandwich, Java has tons of ways to help you join your ingredients together! πŸ₯ͺ

If you have questions, thoughts, or just want to share your favorite joke, drop them in the comments! πŸ˜†πŸ‘‡

Happy Coding! πŸ‘¨β€πŸ’»πŸŽˆ