1

I have the following code:

var days = new string[] { "1", "2", "3", "4" });

I need to convert days to a string as following:

string daysConverted = "'1','2','3','4'";

I tried using string.Join:

var daysConverted = string.Join("','", days); 

but I am getting:

"1','2','3','4"  // missing quotes at the beginning and at the end. 

I can concatenate a quote at both sides but I was wondering if there is any syntax to do it in one shot.

4 Answers 4

7

You should wrap the strings into ' before joining them:

var result = String.Join(",", days.Select(s => "'" + s + "'"));
Sign up to request clarification or add additional context in comments.

6 Comments

Select is not an extension of days since days is string[]
@ehh arrays implement IEnumerable, and the Linq extension method are defined on it, so it should be. Did you include the namespace System.Linq?
You have just increased the number of string concatenations in this way... :-(
@ehh your question was to get any other way around to get the desired output and not the best way, So Domysee gave you the right answer. The accepted answer is still the one which you said you dont want. It may be the best but not the right answer for your question.
@Reddy you are right. Guys sorry for that, if I could accept both answers I was doing it.
|
6

You have to add it at the end and beginning also because Join will only combine two strings. Having said this when combining "1" and "2" with a speaterator of ',' you get "1','2" where the seperator is just added between the two elements.

As of MSDN:

Concatenates all the elements of a string array, using the specified separator between each element.

In the end the following solution seems to be best in terms of memory and speed because you have least number of string-concatenations (two for start and end and one for every element --> 2 + n).

var daysConverted = "'" + string.Join("','", days) + "'"; 

2 Comments

This is the solution I already gave in my question. I was watching for another solution without having to concatenate. Thanks
But this one is faster and uses less memory and don't need Linq ;)
4

Well, speaking about minimal string concatenations, here is a solution utilizing LINQ Aggregate and StringBuilder. It's not so concise, but performs...zero concatenations.

var daysConverted = days.Aggregate(new StringBuilder(), (sb, s) => 
    (sb.Length > 0 ? sb.Append(",") : sb).Append("'").Append(s).Append("'"))
    .ToString(); 

Comments

2

I can concatenate a quote at both sides but I was wondering if there is any syntax to do it in one shot.

Not in an optimized way. string.Join internally uses a StringBuilder to compose the string. Cheating around string.Join would make your code less clear (for example by using tricky LINQ statements) AND slower.

In the end, the classical way that @HimBromBeere suggests is the best one.

4 Comments

Interesting! So may be using string.concatenate() instead of '+' operator is even better?
@ehh The + operator is converted to a string.Concat. See right pane of sharplab.io . The problem is that the string.Concat returns a string, so it has to allocate it. using a string.Concat inside of a string.Join means you are allocating temporary strings just to use them for string.Join.
@xanatos epic! you're working at microsoft visual studio department? :)
@xanatos great info! I meant using string.concatenate in HimBromBeere solution but as you mentioned (and well shown) + operator is converted to a string.Concat.

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.