mengdiwang / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

additional Strings.repeat() methods #531

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
public static String repeat(String str, int count, String separator) {
}
This method is very useful for building sql queries.
Example of use:
 String query = "SELECT * from t WHERE t.id in (" + Strings.repeat("?",10,",") + ")".

Original issue reported on code.google.com by wrom...@gmail.com on 27 Jan 2011 at 12:59

GoogleCodeExporter commented 9 years ago
Joiner.on(',').join(Collections.nCopies(10, "?"))

Original comment by kevinb@google.com on 27 Jan 2011 at 1:22

GoogleCodeExporter commented 9 years ago
Kevin, don't you think that "Joiner.on(',').join(Collections.nCopies(10, "?"))" 
is too verbose?

Making a string of params separated by some delimiter is very common task and 
special Strings.repeat() would be very helpful.

Original comment by kua...@gmail.com on 27 Jan 2011 at 1:31

GoogleCodeExporter commented 9 years ago
I often need to join collections / iterables containing various elements, but I 
never encountered this use case. I can see how it could be useful for SQL 
queries, but I believe Kevin is right here.

Sure [Joiner.on(',').join(Collections.nCopies(10, "?"))] is more verbose than 
[Strings.repeat("?", 10, ",")], but it's also more powerful. For example, say 
you actually want to append your repeated string to a StringBuilder. With 
Joiner, you could do:

StringBuilder sb = ...
Joiner.on(',').appendTo(sb, nCopies(10, "?"));

Moreover, the [Strings.repeat(String str, int count, String separator)] method 
is risky: people might unknowingly invert the "str" and "separator" arguments.

Original comment by nev...@gmail.com on 28 Jan 2011 at 11:29

GoogleCodeExporter commented 9 years ago
Not implementing such a shortcut might also additionally motivate to replace 
risky manual SQL query assembly ;)

Original comment by j...@nwsnet.de on 31 Jan 2011 at 8:52

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:15

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:09