Faisal
Faisal

Reputation: 604

List of string separate with comma and add prefix string to every item of list

I have list like...

List[0] = "Banana"
List[1] = "Apple"
List[2] = "Orange"

I want to produce output as "My-Banana,My-Apple,My-Orange" for that I'm using the following code:

string AnyName = string.Join(",", Prefix + List));

But not getting the expected output, how to add My- before every item?

Upvotes: 11

Views: 7875

Answers (2)

pidizzle
pidizzle

Reputation: 740

First you would want to add the prefix to each element in List like so.

for (var i = 0; i < List.Count; i++)
    List[i] = "My-" + List[i];

Then you would want to split List with commas like this.

var AnyName = String.Join(",", List);

Upvotes: 0

sujith karivelil
sujith karivelil

Reputation: 29036

Are you looking for something like this Example:

listInput[0] = "Apple";
listInput[1] = "Banana";
listInput[2] = "Orange";
string Prefix = "My-";         
string strOutput = string.Join(",", listInput.Select(x=> Prefix + x));
Console.WriteLine(strOutput);

And you will get the output as My-Apple,My-Banana,My-Orange

Upvotes: 23

Related Questions