How to join Strings with a delimeter using the Java join() method

Опубликовано: 07 Октябрь 2024
на канале: Career & Tech HQ
293
8

How to join Strings with a delimeter using the Java join() method

In this video, we will have a look at the Java join() method, which is used to join given Strings using a delimiter and returns a concatenated String.
Let’s use an example to see exactly how this method works
String str1 = “I”;
String str2 = “love”;
String str3 = “programming”;
String str4 = String.join(“/” , str1 , str2 , str3);

String str = String.join(“/” , “I” , “love” , “programming”);
Here the first parameter “/” is called a delimiter and it is used to join the remaining strings passed as parameter
System.out.println(str);
If we run this program, we will get : I/love/programming

This method can also be used to join list elements using a delimiter
Here is an example
List list String = Arrays.asList("Paris", "Vancouver", "Berlin", "Washington");

String cities = String.join(" | ", list);

System.out.println(cities);

This program will return : Paris|Vancouver|Berlin|Washington

#codingriver
#java
#programming