39

Ok, this is a dumb thing that I'm sure I've done dozens of times but for some reason I can't find it.

I have an array... And want to get a string with the contents of that array separated by a delimited...

Where is the .Join() method that I can't find?

(This is .Net 2.0, I don't have any LINQ stuff)

Thank you!

Daniel Magliola
  • 30,898
  • 61
  • 164
  • 243

8 Answers8

49

If you're working with strings, then String.Join is probably what you're looking for.

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Craig
  • 4,323
  • 2
  • 29
  • 32
34

It is on the string class

String.Join(",", new string[] {"a", "b", "c"});

Edit for ints to string

 int[] integers = new int[] { 1,2,3,4,5 };
 String.Join(",", Array.ConvertAll<int, String>(integers, Convert.ToString));
Bob
  • 97,670
  • 29
  • 122
  • 130
4

If you have an array of strings you can call String.join(String, String[]). You can use it even if you don't have an array of strings, you just have to be able to convert your objects to strings

object[] objects = ...
string[] strings = new string[objects.Length];
for (int i = 0; i < objects.Length; i++)
  strings[i] = objects[i].ToString();
string value = String.Join(", ", strings);
Samuel
  • 37,778
  • 11
  • 85
  • 87
  • My friend you are a beautiful person, this is exactly what I needed. I pray you have a good life thanks for saving me hours of work! – Eddy Jawed Aug 07 '18 at 18:34
4
Dim arrStrIds() As String = Array.ConvertAll(arrIntIds, New Converter(Of Integer, String)(
 Function(id As Integer) id.ToString()) )

String.Join(",", arrStrIds)
animuson
  • 53,861
  • 28
  • 137
  • 147
user973754
  • 81
  • 1
  • 5
2

You could use LINQ to Objects and save yourself a few lines

int [] ints = { 0, 1, 2 };
string[] intStrings = (from i in ints select i.ToString()).ToArray<string>();
string joinedStrings = string.Join(",", intStrings);

Oops, Just saw that you don't have LINQ, sorry.

Nick
  • 5,848
  • 4
  • 28
  • 33
1

You can find the method in the String class.

Example using Split and Join:

 public static void Main() { 

    string str = "on two three, four five six."; 
    char[] separators = {' ', '.', ',' }; 

    // Split the string:
    string[] parts = str.Split(separators); 

    string allTogether = String.Join(" | ", parts); 

    Console.WriteLine("Joined: "); 
    Console.WriteLine(allTogether); 


  } 
splattne
  • 102,760
  • 52
  • 202
  • 249
1

you don't need to convert the array into a string array in .NET Framework 4. i don't know about previous Frameworks. so the previous code spends several lines converting your int array into a string array. just skip that step (if it also works in your Framework).

string[] sA = "11,12,13".Split(',');
int[] iA = { 21, 22, 23};
Console.WriteLine(string.Join("+", iA) + " -- " + string.Join("+", sA));

/* displays:
21+22+23 -- 11+12+13
*/
0

If you really like the ergonomics of Array.Join, you can add an Extension Method like this:

public static class MyExtensions {
    public static string Join(this IEnumerable<string> arr, string seperator) =>
        string.Join(seperator, arr);
}

And then use like this:

var list = new string[] {"A", "B", "C"};
var output = list.Join(",");
// "A,B,C"

Demo in .NET Fiddle

KyleMit
  • 30,350
  • 66
  • 462
  • 664