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()));
}
How to add or insert ' (single quotes) for every string in a list in which strings are separated by commas using Java - Stack Overflow
How to list of string can separate with commas and single quotes in apex salesforce - Salesforce Stack Exchange
Best way to convert list to comma separated string in java - Stack Overflow
java - How to add double quotes automatically, converting list of strings as comma separated value - Stack Overflow
for list you can convert this by Java 8
list.stream().collect(Collectors.joining("','", "'", "'"));
import org.apache.commons.lang.StringUtils;
...
...
String arr[] = new String[4];
arr[0] = "my";
arr[1] = "name";
arr[2] = "is";
arr[3] = "baybora";
String join = "'" + StringUtils.join(arr,"','") + "'";
Result:
'my','name','is','baybora'
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
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.
On Android use:
android.text.TextUtils.join(",", ids);
With Java 8:
String csv = String.join(",", ids);
With Java 7-, there is a dirty way (note: it works only if you don't insert strings which contain ", " in your list) - obviously, List#toString will perform a loop to create idList but it does not appear in your code:
List<String> ids = new ArrayList<String>();
ids.add("1");
ids.add("2");
ids.add("3");
ids.add("4");
String idList = ids.toString();
String csv = idList.substring(1, idList.length() - 1).replace(", ", ",");