Java String join()

Before Java 8, the programmer faces the problem when they want to concatenate or join multiple strings by the delimiter. Because the programmer does this work by custom code, they have to write a loop structure to do this. This problem is resolved by Java String join() method.

Java String join()

But Java 8 provides two ways to resolve this problem. First, the StringJoiner class introduced in java 8 that is used to join multiple strings another way is to use Java String join() method.
This method is also introduced in java 8. Let’s see working of Java String join()

This method is used to join the delimiter with string. This method returns a string joined with the given delimiter. You can join multiple strings with the same delimiter. The delimiter is copied for each string. The return type of join() is String. It returns the String joined with the given delimiter.

String.join(delimiter , string1, . . . stringN)

delimiter: The delimiter which you want to copy for each string. string1…stringN: The number of strings that you want to append to with delimiter.

class ExampleOfContains
 {  
	 public static void main(String args[])
	 {    
		String name = "JavaGoal";  			 
		String name1 = "Website";
		String name2 = "Learning";
		String name3 = "Website";
			 
		String joinedString = String.join(";", name, name1, name2, name3);
		System.out.println("Joined with delimiter = "+ (joinedString));
	}
}  

Output: Joined with delimiter = JavaGoal;Website;Learning;Website

Leave a Comment