comma separated values to single inverted quote and comma separated values
Add a single quote at start (^
) & end ($
), and replace comma by quote-comma-quote (like you did) using sed with 3 expressions (using -e
option to specify them):
echo abc,defg,hijklm,op,qrs,tuv | sed -e "s/^/'/" -e "s/\$/'/" -e "s/,/',\'/g"
(also: protect single quotes with double quotes)
results in:
'abc','defg','hijklm','op','qrs','tuv'
in awk
(maybe a little clumsy), generally better since field parsing is handled natively by awk
:
echo abc,defg,hijklm,op,qrs,tuv | awk -F, "{for (i=1;i<=NF-1;i++) printf(\"'%s',\",\$i); printf(\"'%s'\",\$i);}"
(print all fields plus comma except for the last one)
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 );
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('\',\'') + '\'';
split a comma-separated string and add Quotes in C# - elegant solution
You could use string.Join()
here:
var result = string.Join(", ", v.Split(",").Select(x => $"'{x}'"));
Console.WriteLine(result);
// '10', '14', '18', '21'
Which basically concatenates the items by ", "
, then projects single quotes around each string with Enumerable.Select()
from LINQ.
You can also add single quotes without $ - string interpolation:
var result = string.Join(", ", v.Split(",").Select(x => "'" + x + "'"));
As @Racil Hilan helpfully pointed out in the comments, we don't need LINQ here and can just surround the result with "'"
and join on "', '"
:
var result = "'" + string.Join("', '", v.Split(",")) + "'";
PHP adding single quotes to comma separated string
The implode() function requires an array as second argument.
It looks like your $splitnumber is just a simple string not an array of strings as it should probably be.
Try splitting your $splitnumber by commas using the explode() function and put the result of that in the implode function.
Should look like that (not tested):
$splitnumber = ...;
$splittedNumbers = explode(",", $splitnumber);
$numbers = "'" . implode("', '", $splittedNumbers) ."'";
There is probably a cleaner solution by just replacing all occurences of , with ','.
Turn a Comma Delimited String to a List of Strings with Single Quotes
Could you not just use REPLACE to replace each , with a ','?
This assumes the string has the initial and final single quotes in them.
REPLACE(TheString, ',', ''',''')
If not, you could just add them.
'''' + REPLACE(TheString, ',', ''',''') + ''''
join string by comma and put quotes around each element
In a single line using the split
and join
methods with a list comprehension.
s = 'abcd,efgh,igkl,mnop,qrst,uvwx,yz'
print(', '.join([f'"{w}"' for w in s.split(',')]))
# '"abcd", "efgh", "igkl", "mnop", "qrst", "uvwx", "yz"'
How to split an array into single quotes and comma separated list in javascript
Add the quotes into the join
and then wrap the resulting string in quotes as well. No need to add any extra overhead when it can be done more efficiently and be more readable.
"'" + myArray.join("', '") + "'";
The only case where this fails is with an empty array, so you can just test length in that case (the brackets aren't necessary, they're just for readability again):
myArray.length ? ( "'" + myArray.join("', '") + "'" ) : '';
Related Topics
How to Make the Watch Command Interpret Vt100 Sequences
Can't Source Script in a Current Directory
Linux Get Process Name from Pid Within Kernel
What Happened to Socket If Network Has Broken Down
Compare Md5 Sums in Bash Script
How to Build an If Condition in Shell to Check Whether Curl Succeeded
Access Denied to Android.Git.Kernel.Org
Run Multiple Commands At Once in the Same Terminal
Sshpass: Command Not Found Error
Bash Not Trapping Interrupts During Rsync/Subshell Exec Statements
Gdb Warning: Loadable Section Not Found in Added Symbol-File System-Supplied Dso at 0X7Ffff7Ffd000
Scp: How to Find Out That Copying Was Finished