Join Multiple Strings in Java using Delimiter (Java 8)

In this short article, we will discuss four approaches to join multiple strings in Java.
  1. Approach 1 - Using StringBuilder
  2. Approach 2 - Using Java 8 Stream APIs
  3. Approach 3 - Using Java 8 StringJoiner
  4. Approach 4 - Using String.join() method

Join Multiple Strings in Java using Delimiter (Java 8)

package com.java.tutorials.programs;

import java.util.Arrays;
import java.util.StringJoiner;
import java.util.stream.Collectors;

/**
 * Join Multiple Strings in Java using Delimiter
 * @author Ramesh Fadatare
 *
 */
public class Java8Program {

    public static void main(String[] args) {

        String[] strArray = {
            "Banana",
            "Mango",
            "Apple",
            "Watermelon",
            "Grapes"
        };
        String delimiter = "|";

        // Approach 1

        StringBuilder result = new StringBuilder();

        int i = 0;
        for (i = 0; i < strArray.length - 1; i++) {
            result.append(strArray[i]).append(delimiter);
        }

        result.append(strArray[i]);
        System.out.println(" Approach 1 Output => " + result.toString());

        // Approach 2
        String output = Arrays.stream(strArray, 0, strArray.length)
            .collect(Collectors.joining(String.valueOf(delimiter)));

        System.out.println(" Approach 2 Output => " + output);

        // Approach 3
        StringJoiner joiner = new StringJoiner(String.valueOf(delimiter));

        for (String arg: strArray) {
            joiner.add(arg);
        }

        System.out.println(" Approach 3 Output => " + joiner.toString());

        // Approach 4 
        String output1 = String.join(delimiter, strArray);
        System.out.println(" Approach 4 Output => " + output1);
    }
}
Output:
 Approach 1 Output => Banana|Mango|Apple|Watermelon|Grapes
 Approach 2 Output => Banana|Mango|Apple|Watermelon|Grapes
 Approach 3 Output => Banana|Mango|Apple|Watermelon|Grapes
 Approach 4 Output => Banana|Mango|Apple|Watermelon|Grapes

Related Java programs

  1. Java Program to Count Number of Duplicate Words in String
  2. Java Program to Count Number of Words in Given String
  3. Java Program to Count the Number of Occurrences of Substring in a String
  4. Java Program to Count the Occurrences of Each Character in String
  5. Java Program to Merge two String Arrays  
  6. Java Program to Remove Duplicate Words from String 
  7. Java Program to Reverse a String(5 ways)
  8. Java Program to Reverse Each Word of a String    
  9. Java Program to Swap Two Strings      
  10. How to Check if the String Contains only Digits         
  11. How to Check if the String Contains only Letters  
  12. How to Check If the String Contains Only Letters or Digits 
  13. Java Program to Check if Input String is Palindrome 
  14. Java Program to Find all Permutations of String
  15. How to Remove or Trim All White Spaces from a String in Java
  16. How to Remove Leading and Trailing White Space From a String in Java
  17. Java Program to Count Duplicate Characters in a String
  18. Remove Character from String in Java (Java 8)
  19. Java Program to Count Vowels and Consonants in a String (Java 8)
  20. 4 Ways to Find First Non-Repeated Character in String in Java
  21. Java Program to Remove Duplicate Elements in an Array
  22. Java Program to Find Largest Element in an Array
  23. Java Program to Reverse an Array Without Using Another Array
  24. Java Program to Check the Equality of Two Arrays
  25. Java Program to Check Armstrong Number
  26. Java program to check prime number
  27. Java Program to Swap Two Numbers
  28. Java Program to Find Factorial of a Number
  29. Java Program to Reverse a Number
  30. Java Program to Swap Two Strings Without Using Third Variable 

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments