In this short article, we will discuss four approaches to join multiple strings in Java.
- Approach 1 - Using StringBuilder
- Approach 2 - Using Java 8 Stream APIs
- Approach 3 - Using Java 8 StringJoiner
- 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
- Java Program to Count Number of Duplicate Words in String
- Java Program to Count Number of Words in Given String
- Java Program to Count the Number of Occurrences of Substring in a String
- Java Program to Count the Occurrences of Each Character in String
- Java Program to Merge two String Arrays
- Java Program to Remove Duplicate Words from String
- Java Program to Reverse a String(5 ways)
- Java Program to Reverse Each Word of a String
- Java Program to Swap Two Strings
- How to Check if the String Contains only Digits
- How to Check if the String Contains only Letters
- How to Check If the String Contains Only Letters or Digits
- Java Program to Check if Input String is Palindrome
- Java Program to Find all Permutations of String
- How to Remove or Trim All White Spaces from a String in Java
- How to Remove Leading and Trailing White Space From a String in Java
- Java Program to Count Duplicate Characters in a String
- Remove Character from String in Java (Java 8)
- Java Program to Count Vowels and Consonants in a String (Java 8)
- 4 Ways to Find First Non-Repeated Character in String in Java
- Java Program to Remove Duplicate Elements in an Array
- Java Program to Find Largest Element in an Array
- Java Program to Reverse an Array Without Using Another Array
- Java Program to Check the Equality of Two Arrays
- Java Program to Check Armstrong Number
- Java program to check prime number
- Java Program to Swap Two Numbers
- Java Program to Find Factorial of a Number
- Java Program to Reverse a Number
- 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