Home » How to Join Strings in Java

How to Join Strings in Java

selective focus photoraphy of chains during golden hour

Here’s how to join Strings in Java.

public class JoinStrings {

    public static void main(String[] args) {
        // Prints: SELECT id, name, country, gender FROM PEOPLE
        System.out.println(createSql("id", "name", "coutry", "gender"));
    }

    private static String createSql(String... columns) {
        return new StringBuilder("SELECT ")
                .append(String.join(", ", columns))
                .append(" FROM PEOPLE")
                .toString();
    }
}

If you have any questions, leave a comment or ask me on my social media.

Leave a Reply

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