Creating a comma separated list from IList string or IEnumerable string
.NET 4+
IList<string> strings = new List<string>{"1","2","testing"};
string joined = string.Join(",", strings);
Detail & Pre .Net 4.0 Solutions
IEnumerable<string>
can be converted into a string array very easily with LINQ (.NET 3.5):
IEnumerable<string> strings = ...;
string[] array = strings.ToArray();
It's easy enough to write the equivalent helper method if you need to:
public static T[] ToArray(IEnumerable<T> source)
{
return new List<T>(source).ToArray();
}
Then call it like this:
IEnumerable<string> strings = ...;
string[] array = Helpers.ToArray(strings);
You can then call string.Join
. Of course, you don't have to use a helper method:
// C# 3 and .NET 3.5 way:
string joined = string.Join(",", strings.ToArray());
// C# 2 and .NET 2.0 way:
string joined = string.Join(",", new List<string>(strings).ToArray());
The latter is a bit of a mouthful though :)
This is likely to be the simplest way to do it, and quite performant as well - there are other questions about exactly what the performance is like, including (but not limited to) this one.
As of .NET 4.0, there are more overloads available in string.Join
, so you can actually just write:
string joined = string.Join(",", strings);
Much simpler :)
Create a comma separated list from a column
The letters
vector is in-built in R, and paste0
allows you to "collapse" vectors with specified separation characters, in this case a comma:
> paste0(letters[1:5], collapse=",")
[1] "a,b,c,d,e"
It wasn't clear what form these letters had in a dataframe, but if they were simply a column named, say letts
, you could do this:
paste0( dfrm$letts, collapse=",")
shell: build a comma separated list ended with a period
echo -e "abc\ndef\nhij" | sed ':a;N;$!ba;s/\n/, /g'
explained here https://stackoverflow.com/a/1252191/2235381
How create a comma-separated string from items in a list
This is how I do it using join()
in List
:
main(List<String> arguments) {
List cities = ['NY', 'LA', 'Tokyo'];
String s = cities.join(', ');
print(s);
}
How to create comma separated list of numbers based on numeric value in column
You can use df.apply
to apply str.join
on range
object created from 'calc'
:
>>> df['my_comma_list'] = df.calc.apply(lambda x: ', '.join(map(str, range(1, x+1))))
inventory_partner inventory_partner2 calc my_comma_list
0 A1 aa 1 1
1 A2 bb 2 1, 2
2 A3 cc 5 1, 2, 3, 4, 5
3 A4 dd 4 1, 2, 3, 4
4 A5 ee 5 1, 2, 3, 4, 5
5 A6 ff 3 1, 2, 3
Create comma separated list from list of objects
I would avoid reflection if possible.
You can achieve it with compile time safety and readable code easily:
IEnumerable<string> personTexts = listOfPersons
.Select(p => String.Join(",", p.ReferenceNumber, p.FirstName, p.Surname, ...));
string joined = String.Join(Environment.NewLine, personTexts);
You have full control over which property should be used and in which order.
Related Topics
Sqlite Alter Table Add Multiple Columns in a Single Statement
Normalization in Plain English
Delete Parent If It's Not Referenced by Any Other Child
SQL Update Order of Evaluation
How to Write a SQL Delete Statement with a Select Statement in the Where Clause
SQL for Applying Conditions to Multiple Rows in a Join
Difference Between Right & Left Join VS Right & Left Outer Join in SQL
Max Length for Client Ip Address
How to Select Even Records from a Table in Oracle
Hive Query Performance for High Cardinality Field
Does the Jdbc Spec Prevent '' from Being Used as an Operator (Outside of Quotes)
How to Store a List in a Db Column
Sql: Capitalize First Letter Only
Why Do Null Values Come First When Ordering Desc in a Postgresql Query
Return Multiple Columns and Rows from a Function Postgresql Instead of Record
How to Select Every Row Where Column Value Is Not Distinct
Is There a Nesting Limit for Correlated Subqueries in Some Versions of Oracle