0

I had an issue on using string interpolation in string.join

    string type1 = "a,b,c";
    string[] type2 = new string[3] { "a", "b", "c" };

how to write the string.Join query, below code is just my attempt, but the result is not as expected.

    string result = string.Join(",", $"'{type1}'");

In both cases, the output should be 'a','b','c'

how to use string.Join() with string interpolation for an array of strings

2
  • @41686d6564 well, technically a string is an enumerable. However I doubt that´s what OP wants. Commented Aug 12, 2020 at 7:53
  • var bob = string.Join(",", type2.Select(z => $"'{z}'")); dotnetfiddle.net/wKYbz8 Commented Aug 12, 2020 at 8:40

1 Answer 1

3

If you want to add single quotes around each elements, you can apply a simple .Select() sequence, such as :

var type1 = "a,b,c";
var type2 = new string[3] { "a", "b", "c" };

// using System.Linq;
var result1 = string.Join(',', type1.Split(',').Select(x => $"'{x}'"));
var result2 = string.Join(',', type2.Select(x => $"'{x}'"));

Console.WriteLine($"Type1 : {result1}");
Console.WriteLine($"Type2 : {result2}");

This outputs :

Type1 : 'a','b','c'
Type2 : 'a','b','c'

Fiddle

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

2 Comments

Yep, based on the OP's comment to the other answer, it looks like that's what they want.
Well, even based on OP's question

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.