List<String> nameList = ...
String result = nameList.stream().collect(Collectors.joining("','", "'", "'"));
Answer from Igorock on Stack OverflowList<String> nameList = ...
String result = nameList.stream().collect(Collectors.joining("','", "'", "'"));
I know this is a really old post but just in case someone is looking for how you could do this in a Java 8 way:
private String join(List<String> namesList) {
return String.join(",", namesList
.stream()
.map(name -> ("'" + name + "'"))
.collect(Collectors.toList()));
}
You can do it in two steps with StringUtils only,
List<String> s = new ArrayList<>();
s.add("one");
s.add("two");
s.add("three");
String step1 = StringUtils.join(s, "\", \"");// Join with ", "
String step2 = StringUtils.wrap(step1, "\"");// Wrap step1 with "
System.out.println(step2);
Output,
"one", "two", "three"
BUT
I need to pass them in a mongo DB query when using $in operator
For mongodb query you don't need to build it this way, specifically in case of $in you can query documents in following way,
BasicDBObject yourInQuery = new BasicDBObject();
yourInQuery.put("in_column", new BasicDBObject("$in", yourList));
DBCursor cursor = collection.find(yourInQuery);
Please read more about this in following link,
- Find or Query Data with Java Driver
String joined = s.stream()
.map(plain -> '"' + StringEscapeUtils.escapeJava(plain) + '"')
.collect(Collectors.joining(", "));
The idea is to first convert each of the strings to a properly quoted representation and then join them.
You have to decide for yourself which escaping function to use. And just in case you are generating a CSV file, you should really use some CsvWriter class.
Since Java 8:
String.join(",", slist);
From Apache Commons library:
import org.apache.commons.lang3.StringUtils
Use:
StringUtils.join(slist, ',');
Another similar question and answer here
You could count the total length of the string first, and pass it to the StringBuilder constructor. And you do not need to convert the Set first.
Set<String> abc = new HashSet<String>();
abc.add("A");
abc.add("B");
abc.add("C");
String separator = ", ";
int total = abc.size() * separator.length();
for (String s : abc) {
total += s.length();
}
StringBuilder sb = new StringBuilder(total);
for (String s : abc) {
sb.append(separator).append(s);
}
String result = sb.substring(separator.length()); // remove leading separator
Existing example and ones with replacing quotes can break if there are quotes in the strings themselves. You can use JSONArray to parse it and then convert to a list if needed
String x = "[\"role_A\",\"role_B\",\"role_C\"]";
JSONArray arr = new JSONArray(x);
List<String> list = new ArrayList<String>();
for (Object one : arr) {
list.add((String)one);
}
System.out.println(list); //prints [role_A, role_B, role_C]
System.out.println(list.size()); //prints 3
json.jar can be found in the Maven repo
String s = "[\"role_A\",\"role_B\",\"role_C\"]";
String[] res = s.split("\\[")[1].split(",");
for (String str : res) {
str = str.replace("]", "").replace("\"", "");
}
Resulting array res is your desired array.
Either write a simple method yourself, or use one of the various utilities out there.
Personally I use apache StringUtils (StringUtils.join)
edit: in Java 8, you don't need this at all anymore:
String joined = String.join(",", name);
Android developers are probably looking for TextUtils.join
Android docs: http://developer.android.com/reference/android/text/TextUtils.html
Code:
String[] name = {"amit", "rahul", "surya"};
TextUtils.join(",",name)