How to Improve Code That Quotes All Array Elements with ''' and Returns a String Containing All Those Quoted and Comma-Separated Elements

How to add or insert ' (single quotes) for every string in a list in which strings are separated by commas using Java

  1. Iterate the list (for/while).

  2. For each element in the list append <element-of-list>. Hint: use append() on StringBuilder.

  3. 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



Leave a reply



Submit