1

I have a string array. It is dynamic and can be of any length(0 also). How can I make a single string from the array, delimited by any separator like ; or | ??

string str = string.empty;
string[] arrOptions = strOptions.Split(new string[]{"\n"}, StringSplitOptions.RemoveEmptyEntries);

Now, have to make the string from arrOptions and put it in str

3
  • Thanks All. But I think I should select the first answer, that should be fair enough. ;) Commented Jan 17, 2013 at 9:02
  • :):):):) how did you determine which answer was the first? Commented Jan 17, 2013 at 9:10
  • @KonstantinVasilcov : I saw from my answer notifications in inbox ;) Commented Jan 17, 2013 at 9:14

6 Answers 6

5

Use string.Join:

string result = string.Join("\n", arrOptions);

Or simply concat them, if you don't need the separator anymore:

string result = string.Concat(arrOptions);
Sign up to request clarification or add additional context in comments.

4 Comments

Concat cannot operate dynamic numbers of string elements
@Avishek what do you mean? public static string Concat(params string[] values) this overload is used in this case. What's wrong with it?
Remember, I also want a separator. :/
@Avishek then it's definitely String.Join Method
3

Use String.Join(separator, objects) method.

Comments

2

You can try both way:

    string[] strArr = { "Abc", "DEF", "GHI" };

    //    int i = 0;
    //    string final=string.Empty;
    //IterationStart:
    //    if (i < strArr.Length)
    //    {
    //        final += strArr[i] + ",";
    //        i++;
    //        goto IterationStart;
    //    }
    //Console.WriteLine(final);

     string str = string.Join(",", strArr);
     Console.WriteLine(str);

1 Comment

commented code is another way to achieve this. yes i am using labeling.
2
str = string.Join( ';', arrOptions );

Comments

1

Try : string.Join(seperator, arrOptions);

Comments

0

You might be looking for the below solution.

string str = string.empty;
string[] arrOptions = strOptions.Split(new string[]{"\n"}, StringSplitOptions.RemoveEmptyEntries);
str = string.Concat(arrOptions);

Thanks,

Praveen

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.