Thursday, September 4, 2025
HomeLanguagesJavaJava Guava | Chars.join() method with Examples

Java Guava | Chars.join() method with Examples

The join() method of Chars Class in the Guava library is used to combine or join all the given char values separated by a separator. These char values are passed a parameter to this method. This method also takes the separator as the parameter. This method returns a String which is the result of join operation on the specified char values.

For example: join(“-“, 1L, 2L, 3L) returns the string “1-2-3”.

Syntax:

public static String join(String separator, char… array)

Parameters: This method accepts two mandatory parameters:

  • separator: which is the character that occurs in between the joined char values.
  • array: which is an array of char values that are to be joined.

Return Value: This method returns a string containing all the given char values separated by separator.

Below programs illustrate the use of this method:

Example 1:




// Java code to show implementation of
// Guava's Chars.join() method
  
import com.google.common.primitives.Chars;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating a char array
        char[] arr = { 'a', 'b', 'c', 'd', 'e' };
  
        // Using Chars.join() method to get a
        // string containing the elements of array
        // separated by a separator
        System.out.println(Chars.join("#", arr));
    }
}


Output:

a#b#c#d#e

Example 2:




// Java code to show implementation of
// Guava's Chars.join() method
  
import com.google.common.primitives.Chars;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating a char array
        char[] arr = { 'G', 'E', 'E', 'K', 'S' };
  
        // Using Chars.join() method to get a
        // string containing the elements of array
        // separated by a separator
        System.out.println(Chars.join("*", arr));
    }
}


Output:

G*E*E*K*S

Reference: https://google.github.io/guava/releases/18.0/api/docs/com/google/common/primitives/Chars.html#join(java.lang.String, %20char…)

RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS