Several ways to concatenate strings separated by commas in Java

I sometimes want to concatenate arrays and lists with comma-separated strings in Java, and I wondered if there is a way other than the StringJoiner class, so I made a note. Eventually I used StringUtils # join (). If it was a String array, String # join () would have been fine, but since it was an int array, I chose StringUtils.

From here on, I will introduce what I have investigated.

Method using String Joiner

The StringJoiner class is available from Java 7. If it is not a String type array, you need to convert it to a String type.

import java.util.StringJoiner;
import java.util.Arrays;

public class StringJoinerSample {
  public static void main(String args[]) {
    int[] arr = {1, 2, 3, 4, 5};
    StringJoiner sj = new StringJoiner(",");
    Arrays.stream(arr).forEach(i -> sj.add(String.valueOf(i)));
    System.out.println(sj.toString()); // => 1,2,3,4,5
  }
}

Method using String # join ()

If it is an array of String, you can use String # join (). String # join () can be used from Java 8.

public class MyClass {
    public static void main(String args[]) {
        String[] arr = {"en", "ja", "fr", "es", "zh"};
        String str = String.join(",", arr);
        System.out.println(str); // => en,ja,fr,es,zh
    }
}

You can also take a List as an argument.

import java.util.Arrays;
import java.util.List;

public class MyClass {
  public static void main(String args[]) {
    List<String> list = Arrays.asList("en", "ja", "fr", "es", "zh");
    String str = String.join(",", list);
    System.out.println(str); // => en,ja,fr,es,zh
  }
}

Method using Collectors # joining

If it is a collection of type List , it can be concatenated as follows.

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class MyClass {
  public static void main(String args[]) {
    List<String> list = Arrays.asList("en", "ja", "fr", "es", "zh");
    String str = list.stream().collect(Collectors.joining(","));
    System.out.println(str); // => en,ja,fr,es,zh
  }
}

You can also enclose the front and back in parentheses ().

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class MyClass {
  public static void main(String args[]) {
    List<String> list = Arrays.asList("en", "ja", "fr", "es", "zh");
    String str = list.stream().collect(Collectors.joining(",", "(", ")"));
    System.out.println(str); // => (en,ja,fr,es,zh)
  }
}

Method using StringUtils # join ()

The StringUtils class is in org.apache.commons: commons-lang3. You need to install the library with gradle or maven.

org.apache.commons:commons-lang3:3.6

You can write it in one line as follows. Since StringUtils # join () takes an array of Object as an argument I am using ʻArrayUtils # toObject () `to convert to an array of Integer class.

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

public class MyClass {
    public static void main(String args[]) {
        int[] arr = {1,2,3,4,5};
        String str = StringUtils.join(ArrayUtils.toObject(arr), ",");
        System.out.println(str); // => 1,2,3,4,5
    }
}

Another option is to use Google Guava's Joiner class.

Thank you for reading for me until the end. If you have any deficiencies or questions, please use the comments section, edit request, Twitter, etc. I would appreciate it if you could tell me if there is another way.

Recommended Posts

Several ways to concatenate strings separated by commas in Java
How to concatenate strings in java
How to connect the strings in the List separated by commas
Concatenate strings returned by methods of multiple objects in Java Stream
[Java] How to receive numerical data separated by spaces in standard input
Two ways to start a thread in Java + @
How to concatenate strings
Differences in how to handle strings between Java and Perl
Cast an array of Strings to a List of Integers in Java
Determine if the strings to be compared are the same in Java
Multithreaded to fit in [Java] template
How to learn JAVA in 7 days
Log output to file in Java
How to use classes in Java?
How to name variables in Java
Try to implement Yubaba in Java
[Java] Java was said to be okay to concatenate strings with +, so I checked
How to implement Kalman filter in Java
Try to solve Project Euler in Java
Easy to make Slack Bot in Java
Java reference to understand in the figure
Try to implement n-ary addition in Java
Change List <Optional <T >> to Optional <List <T >> in Java
Convert SVG files to PNG files in Java
How to embed Janus Graph in Java
How to get the date in java
Add footnotes to Word documents in Java
[Java Bronze] 5 problems to keep in mind
Sample to unzip gz file in Java
Java to C and C to Java in Android Studio
Add SameSite attribute to cookie in Java
[Java] Various methods to acquire the value stored in List by iterative processing