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?

2

2 Answers 2

23

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

Sign up to request clarification or add additional context in comments.

1 Comment

yeah Thanks Exactly what i am looking for
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.