How to add or insert ' (single quotes) for every string in a list in which strings are separated by commas using Java
Iterate the list (for/while).
For each element in the list append
<element-of-list>
. Hint: useappend()
onStringBuilder
.Truncate/substring the list to remove the last element added. Hint: use substring on
String
class.
javascript array as a list of strings (preserving quotes)
The quotes aren't preserved because they're not actually part of the string value, they're just necessary to indicate string literals in your code.
So, don't use toString()
. Instead, one way to do it is as follows:
var arr = ['item1','item2','item3','item4'];
var quotedAndCommaSeparated = "'" + arr.join("','") + "'";
// quotedAndCommaSeparated === "'item1','item2','item3','item4'"
The Array.join() method returns a string that is all of the array elements concatenated into a single string with an (optional) separator between each item. So if you specify a separator that includes the quotation marks and commas you just have to manually append a starting and ending quote for the first and last item (respectively).(And please tell me you're not using client-side JavaScript to form your SQL.)
EDIT: to allow for an empty array, include a default value for the resulting string, otherwise (as pointed out by missingno) the string would be "''"
:
var newString = arr.length === 0 ? "" : "'" + arr.join("','") + "'";
// default for empty array here ---^^
(Might be more appropriate to have an if (arr.length===0)
to take some other action rather than running the SELECT statement.) join array enclosing each value with quotes javascript
Use Array.map to wrap each entry in quotes and then join them.
var dates = ['1/2/12','15/5/12'];
const datesWrappedInQuotes = dates.map(date => `'${date}'`);
const withCommasInBetween = datesWrappedInQuotes.join(',')
console.log( withCommasInBetween );
PHP add single quotes to comma separated list
Use '
before and after implode()
$temp = array("abc","xyz");
$result = "'" . implode ( "', '", $temp ) . "'";
echo $result; // 'abc', 'xyz'
In C#: Add Quotes around string in a comma delimited list of strings
string s = "A,B,C";
string replaced = "'"+s.Replace(",", "','")+"'";
Thanks for the comments, I had missed the external quotes.Of course.. if the source was an empty string, would you want the extra quotes around it or not ? And what if the input was a bunch of whitespaces... ? I mean, to give a 100% complete solution I'd probably ask for a list of unit tests but I hope my gut instinct answered your core question.
Update: A LINQ-based alternative has also been suggested (with the added benefit of using String.Format and therefore not having to worry about leading/trailing quotes):
string list = "Fred,Sam,Mike,Sarah";
string newList = string.Join(",", list.Split(',').Select(x => string.Format("'{0}'", x)).ToList());
How to wrap comma separated values string in single quotes?
You can do it like below. Hope it helps.
var input = "label1, label2, label3, label4, label5";
var result = '\'' + input.split(',').join('\',\'') + '\'';
Separating an Array into a comma separated string with quotes
This functionality is built into ActiveRecord:
Model.where(:my_field => ['blue','green','red'])
Adding quotes to a comma separated string
Try this
var a = "1.1.1.1,2.2.2.2,3.3.3.3";
var b = "'" + a.split( "," ).join( "','" ) + "'";
console.log( b );
Related Topics
Using Paperclip Within Seeds.Rb
Sqlite3::Sqlexception: Duplicate Column Name While Migrating
Save PDF File Shown by Pdfkit Middleware
Puma and Nginx 502 Bad Gateway Error (Ubuntu Server 14.04)
How to Handle Single Table Inheritance in Simpleform So a Single Helper Handles All Models
Rails and JavaScript Minifier and Aggregator
Overriding Instance Variable Array's Operators in Ruby
I Need to Go from HTML to Markdown, Any Suggestion
Stripping Commas from Integers or Decimals in Rails
Does Rails Support a Neat Way of Listening to a Udp Socket
Using Update_Columns in Rails 3
Error: Spawn Enoent: Missing The SASS Executable
Ruby: Class C Includes Module M; Including Module N in M Does Not Affect C. What Gives
Download a Carrierwave Upload from S3