11

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?

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Faisal
  • 584
  • 3
  • 11
  • 33

2 Answers2

22

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

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
0

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);

mjwills
  • 23,389
  • 6
  • 40
  • 63
pidizzle
  • 720
  • 6
  • 12