How to Use Java String Class join() Method

String join() Method

Function Description

The Java String class join() method is used to concatenate the specified CharSequence arguments together using the specified delimiter. It is a static method and returns a concatenated string. If an argument is passed as a null element, it concatenates the “null” as a string. In String class join() method is defined as an overloaded method.

  1. Method to join CharSequence elements:-> This method is used to concatenate the specified charsequences together and returns the new concatenated string.

  2. Method to join Iterable object’s CharSequence elements:-> This method  is used to concatenate  together the specified charsequences specified in iterable object and returns the new concatenated string

Method Signature

  1. Method to join passing CharSequence elements

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

  2. Method to join Iterable object’s CharSequence elements

    public static String join(CharSequence delimiter, Iterable<? extends CharSequence> iterableElements)

Parameters

delimiter: It is a sequence of characters, that is used to separate each of the specified elements in the returned concatenated string

elements: The sequence of characters to be concatenated together.

iterableElements: It is a list of CharSequence type elements iterable object. These CharSequence elements are concatenated together to return the concatenated string.

Return Type

String Object. It returns a new concatenated String separated by the specified delimiter.

Exception

Throws NullPointerException if delimiter or element(s) is null.

Examples of Java String Class join() Method

1. Example of Java String Class join()  method to concatenate the passing CharSequence elements

public class ExampleStringJoin {
 
    private void getDateString() {

          String day = "12";

          String month = "02";

          String year = "2020";
 
          // return joined arguments separated by delimiter forward slash(/)
 
          String dateString = String.join("/", day, month, year);

          System.out.println("Date = " + dateString);
    }
 
    public static void main(String[] args) {

          ExampleStringJoin example = new ExampleStringJoin();

          example.getDateString();

     }
 
}
 

Output:

Date = 12/02/2020

2.  Example of Java String Class join() method to concatenate Iterable object’s CharSequence elements

import java.util.ArrayList;

import java.util.List;
 
public class ExampleStringJoin {
 
    private void getWelComeStrings() {

          List<String> list = new ArrayList<String>();

          list.add("Welcome");

          list.add("to");

          list.add("String");

          list.add("join");

          list.add("function");

          list.add("learning");
 
          // return joined string of list separated by space delimiter

          String finalString = String.join(" ", list);

          System.out.println("String after join : " + finalString);

    }
 
 
    public static void main(String[] args) {

          ExampleStringJoin example = new ExampleStringJoin();

          example.getWelComeStrings();

     }
 
}
 

Output:

String after join : Welcome to String join function learning

3.  Join function throw NullPointerException while delimiter is null

package com.java.test;
 
public class ExampleStringJoin {
 
    private void getDateString() {

          String day = "12";

          String month = "02";

          String year = "2020";
   
          // return joined arguments separated by with delimiter forward slash(/)
        
         String dateString = String.join(null, day, month, year);

         System.out.println("Date = " + dateString);
    }
 
   public static void main(String[] args) {

   ExampleStringJoin exampleStringJoin = new ExampleStringJoin();
   
   exampleStringJoin.getDateString();
   
    }
 
}
 

Output:

Exception in thread "main" java.lang.NullPointerException

    at java.util.Objects.requireNonNull(Objects.java:203)
 
    at java.lang.String.join(String.java:2451)

    at com.java.test.ExampleStringJoin.getDateString(ExampleStringJoin.java:11)

    at com.java.test.ExampleStringJoin.main(ExampleStringJoin.java:17)

Other Methods of String Class

SequenceMethodDescription
1char charAt(int index)This method is used to return the character of the string at specified index. First character is at index 0 while last is at string length-1 . StringIndexOutOfBounds Exception is thrown if valid index value is not passed.
2int length()Returns the number of characters count  in  a string.
3static String format(String format, Object… args)Return the formatted String. It is a static method and formats the string as per passing format and argument(s).
4static String format(Locale l, String format, Object… args)Return the formatted String. It is a static method and formats the string as per passing format and argument(s) and locale.
5String substring(int beginIndex)Returns substring from the given index position to the end index of the string.
6String substring(int beginIndex, int endIndex)Returns substring from the specified begin index to the specified end index.
7boolean contains(CharSequence s)Returns boolean value(true/false). It searches the given sequence of characters within the string and returns true/false accordingly. Characters will be case sensitive while searching in string.
8boolean equals(Object another)Returns boolean value(true/false). It checks the equality of string with the passing object.
9boolean isEmpty()Returns boolean value(true/false). It returns true if the string is empty otherwise returns false.
10String concat(String str)Returns the concatenating string. It concatenates the passing string at the end of the string.
11String replace(char old, char new)Return the replaced string. It replaces all occurrences of the specified character with the new character.
12String replace(CharSequence old, CharSequence new)Return the replaced string. It replaces all occurrences of the specified Character sequence with the specified new Character sequence.
13String replaceAll(String regex, String replacement)Returns the replaced string. It replaces all occurrences of the sequence of characters with the given replacement string that are matching with the given regular expression.
14String replaceFirst(String regex, String replacement)Returns the replaced string. It replaces the first matching sequence of characters with the replacement string that matches with the given regular expression.
15static boolean equalsIgnoreCase(String another)Returns the boolean (true/false) value. It compares the passing string with string by ignoring the case. It  compares the content irrespective of lowercase or uppercase of the character(s).
16String[] split(String regex)Returns the array of strings resulting from splitting of the string     around matches of the given regular expression. String is breaked into strings from all  the matching regular expressions.
17String[] split(String regex, int limit)Returns the array of strings resulting from splitting of the string     around matches of the given regular expression. It breaks the string from all the matching regular expression upto the given limit.
18String intern()Returns an interned string. This method helps to compare two objects using == operator and checks in a string constant pool. it is faster for comparison.
19int indexOf(int ch)Returns the index value of first occurrence of the specified character within the string
20int indexOf(int ch, int fromIndex)Returns the index value of first occurrence of the specified character from the substring starting from specified index
21int indexOf(String substring)Returns the index value of first occurrence of the specified substring within the string.
22int indexOf(String substring, int fromIndex)Returns the index value of the first occurrence of the specified string within the substring starting from specified index to end of the string index.
23int lastIndexOf(int ch)Returns the index value of last occurrence of the specified character
24int lastIndexOf (int ch, int fromIndex)Returns the index value of last occurrence of the specified character, search starts at given fromIndex. 
25int lastIndexOf (String str)Returns the index value of last occurrence of the specified string.
26int lastIndexOf (String str, int fromIndexReturns the index value of last occurrence of the specified string, searches start from given fromIndex
27String toLowerCase()Returns all the characters of the string in lowercase .
28String toLowerCase(Locale l)Returns all the characters of the string in lowercase using specified locale.
29String toUpperCase()Returns all the characters of the string in uppercase.
30String toUpperCase(Locale l)Returns all the characters of the string in uppercase using specified locale.
31String trim()Returns the string after removing the white space from the beginning and end of the string.
32static String valueOf(int value)Return the converted string. It converts the objects and all primitive types of values into string. It is a static method.

In this article, we learned all about join() method of String class with examples. Apart from join() method we got an idea about other String methods too. We will come up with more articles explaining other String methods with examples. For more details please click on the respective methods link.

Leave a Reply

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