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.
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
}
}
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
}
}
If it is a collection of type List
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)
}
}
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