Assume that I have a list of Strings.
List<String> s = new ArrayList<>();
s.add("one");
s.add("two");
s.add("three");
When I use StringUtils.join(",", s)
it gives me the result as
"one, two, three"
Whereas I need the output as
"one","two","three"
We don't like to use Guava utility as the project is not in active state.
Is it possible via Apache Commons utility?
How can I achieve this via utility instead of writing my own logic to do the same?
split("(? =\"[^\"]. *\")");
The first method to print the double quotes with the string uses an escape sequence, which is a backslash ( \ ) with a character. It is sometimes also called an escape character. Our goal is to insert double quotes at the starting and the ending point of our String.
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"
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,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With