Java String join() Method

The String.join() method in Java is used to join multiple strings together with a specified delimiter. This method is part of the String class and provides a convenient way to concatenate strings with a delimiter, making it easy to create comma-separated values, URL parameters, and other formatted strings.

Table of Contents

  1. Introduction
  2. join Method Syntax
  3. Examples
    • Basic Usage
    • Joining Array of Strings
    • Handling Edge Cases
    • Real-World Use Case
  4. Conclusion

Introduction

The String.join() method is a member of the String class in Java. It allows you to concatenate multiple strings with a specified delimiter. This method is useful for creating formatted strings and can simplify code that involves string concatenation with delimiters.

join() Method Syntax

There are two overloaded versions of the join method:

Joining CharSequence Elements

public static String join(CharSequence delimiter, CharSequence... elements)
  • delimiter: The delimiter that separates each element.
  • elements: The elements to be joined.

Joining Iterable Elements

public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
  • delimiter: The delimiter that separates each element.
  • elements: An Iterable of elements to be joined.

Examples

Basic Usage

The join method can be used to join multiple strings with a delimiter.

Example

public class JoinExample {
    public static void main(String[] args) {
        String result = String.join(", ", "Apple", "Banana", "Cherry");
        System.out.println("Joined string: " + result);
    }
}

Output:

Joined string: Apple, Banana, Cherry

Joining Array of Strings

The join method can be used to join elements of an array with a delimiter.

Example

public class JoinArrayExample {
    public static void main(String[] args) {
        String[] fruits = {"Apple", "Banana", "Cherry"};
        String result = String.join(" | ", fruits);
        System.out.println("Joined string: " + result);
    }
}

Output:

Joined string: Apple | Banana | Cherry

Handling Edge Cases

Example: Joining No Elements

If no elements are provided, the join method returns an empty string.

public class JoinNoElementsExample {
    public static void main(String[] args) {
        String result = String.join(", ");
        System.out.println("Joined string: '" + result + "'");
    }
}

Output:

Joined string: ''

Example: Joining with Empty Strings

The join method correctly handles empty strings within the elements.

public class JoinEmptyStringsExample {
    public static void main(String[] args) {
        String result = String.join(", ", "Apple", "", "Cherry");
        System.out.println("Joined string: " + result);
    }
}

Output:

Joined string: Apple, , Cherry

Real-World Use Case

Example: Creating a CSV Line

One common use case for join is creating a comma-separated values (CSV) line from an array of strings.

public class CreateCSVLineExample {
    public static void main(String[] args) {
        String[] data = {"Alice", "30", "Engineer"};
        String csvLine = String.join(",", data);
        System.out.println("CSV line: " + csvLine);
    }
}

Output:

CSV line: Alice,30,Engineer

Example: Constructing a URL with Parameters

Another common use case for join is constructing a URL with query parameters.

import java.util.Map;
import java.util.StringJoiner;

public class ConstructURLExample {
    public static void main(String[] args) {
        Map<String, String> params = Map.of("q", "Java", "sort", "relevance", "page", "1");
        String baseUrl = "https://www.example.com/search";
        String url = constructURL(baseUrl, params);
        System.out.println("Constructed URL: " + url);
    }

    public static String constructURL(String baseUrl, Map<String, String> params) {
        StringJoiner joiner = new StringJoiner("&", baseUrl + "?", "");
        for (Map.Entry<String, String> entry : params.entrySet()) {
            joiner.add(entry.getKey() + "=" + entry.getValue());
        }
        return joiner.toString();
    }
}

Output:

Constructed URL: https://www.example.com/search?q=Java&sort=relevance&page=1

In this example, the StringJoiner class is used alongside String.join() to create a URL with query parameters.

Conclusion

The String.join() method in Java is a powerful and convenient tool for concatenating multiple strings with a specified delimiter. It simplifies the process of creating formatted strings, such as CSV lines or URLs with query parameters. By understanding and utilizing the join method, you can efficiently manage string concatenation tasks in your Java programs.

Leave a Comment

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

Scroll to Top