BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java String join() method explained with examples

By Chaitanya Singh | Filed Under: java

In Java 8 we have a new Method join() in the Java String class. Java String join() method concatenates the given Strings and returns the concatenated String. Java 8 also introduced a new StringJoiner class for the same purpose.

Java String Join() method Signature

public static String join(CharSequence delimiter,
                          CharSequence... elements)

Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.
For example,

String message = String.join("-", "This", "is", "a", "String");
// message returned is: "This-is-a-String"

The first argument of this method specifies the delimiter that is used to join multiple strings.
Note that if an element is null, then “null” is added.

Java String join() example

public class Example{  
   public static void main(String args[]){  
	//The first argument to this method is the delimiter
	String str=String.join("^","You","are","Awesome");  
	System.out.println(str);  
   }
}

Output:

You^are^Awesome

Java String join() Example to join list elements by a delimiter

In this example we are joining the elements of a List by a delimiter using join() method.

import java.util.List;
import java.util.Arrays;
public class Example{  
   public static void main(String args[]){  
	//Converting an array of String to the list
	List list<String> = Arrays.asList("Steve", "Rick", "Peter", "Abbey");
	String names = String.join(" | ", list);
	System.out.println(names);
   }
}

Output:

Steve | Rick | Peter | Abbey

Related Posts:

  1. Java String concat() example
  2. Java substring() example
  3. Java String copyValueOf() example
  4. Java String intern() example

Reference

String join() method – JavaDoc

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2023 BeginnersBook . Privacy Policy . Sitemap