dcsimg
LinkedIn
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Jul 7, 2021

How to Use the StringJoiner Class in Java

This Java programming tutorial showcases how to join strings using Java's StringJoiner Class. It is a pretty straightforward method and can be used to join multiple strings together. Here is the example code showing how to join strings in Java. We include parameters to better format the output:

*/

import java.util.StringJoiner;

public class StringJoinerEx 
{
	public static void main(String args[])
	{
		StringJoinerEx stringJoinerEx = new StringJoinerEx();
		stringJoinerEx.proceed();
	}

	public void proceed() {
		
		StringJoiner stringJoiner = new StringJoiner(":", "[", "]");
		stringJoiner.add("Bill").add("Larry").add("Steve").add("Tim");
		String joinerFormattedString = stringJoiner.toString();
		
		System.out.println("Strings joined using StringJoiner: " + joinerFormattedString);
	}
}

/*

The following output gets printed to the console when you run this Java program:

$java StringJoinerEx
Strings joined using StringJoiner: [Bill:Larry:Steve:Tim]
MS Sridhar
 
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date