+++ Program that joins List of strings (C# .NET 4.0) +++
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Create a List of three strings.
var list = new List<string>() { "cat", "dog", "rat" };
// Join the strings from the List.
string joined = string.Join<string>("*", list);
// Display.
Console.WriteLine(joined);
}
}
1.
IEnumerable<long> ids = new long[]{1,3,4,5};
2.
string delimitedIds = string.Join(",", ids.Select(x => x.ToString()).ToArray());
// Array ToString "," separated
int[] intArray = { 1, 2, 3 };
Console.WriteLine(intArray.ToString(","));
// output: 1,2,3
// ArrayList ToString ":" separated
ArrayList arrayList = new ArrayList() { 1, 2, 3 };
Console.WriteLine(arrayList.ToString(":"));
// output 1:2:3
// A class object List ToString " - " separated
List<Foo> foos = new List<Foo>()
{
new Foo() { Name = "foo1", Number = 1 },
new Foo() { Name = "foo2", Number = 2 },
new Foo() { Name = "foo3", Number = 3 },
};
Console.WriteLine(foos.ToString(" - "));
// output 'foo1 1' - 'foo2 2' - 'foo3 3'
// A struct List ToString "||" separated
List<StructFoo> sfoos = new List< StructFoo >()
{
new StructFoo() { Name = "sfoo1", Number = 1 },
new StructFoo() { Name = "sfoo2", Number = 2 },
new StructFoo() { Name = "sfoo3", Number = 3 },
};
Console.WriteLine(sfoos.ToString("||"));
// output 'sfoo1 1'||'sfoo2 2'||'sfoo3 3'
// A generic dictionary ToString "," separated
Dictionary< int, Foo > dictionary = new Dictionary< int, Foo >()
{
{ 1, new Foo() { Name = "foo1", Number = 1 }},
{ 2, new Foo() { Name = "foo2", Number = 2 }},
{ 3, new Foo() { Name = "foo3", Number = 3 }},
};
Console.WriteLine(dictionary.ToString(","));
// output: [1, 'foo1 1'],[2, 'foo2 2'],[3, 'foo3 3']
// string as IEnumerable char
string text = "abcdefg";
Console.WriteLine(text.ToString(","));
// output: a,b,c,d,e,f,g
Hiç yorum yok:
Yorum Gönder