I have written some extensions to accommodate almost any need.
There are extension overloads to feed with Separator, String.Format and IFormatProvider.
Example:
var array1 = new byte[] { 50, 51, 52, 53 };
var array2 = new double[] { 1.1111, 2.2222, 3.3333 };
var culture = CultureInfo.GetCultureInfo("ja-JP");
Console.WriteLine("Byte Array");
//Normal print
Console.WriteLine(array1.StringJoin());
//Format to hex values
Console.WriteLine(array1.StringJoin("-", "0x{0:X2}"));
//Comma separated
Console.WriteLine(array1.StringJoin(", "));
Console.WriteLine();
Console.WriteLine("Double Array");
//Normal print
Console.WriteLine(array2.StringJoin());
//Format to Japanese culture
Console.WriteLine(array2.StringJoin(culture));
//Format to three decimals
Console.WriteLine(array2.StringJoin(" ", "{0:F3}"));
//Format to Japanese culture and two decimals
Console.WriteLine(array2.StringJoin(" ", "{0:F2}", culture));
Console.WriteLine();
Console.ReadLine();
Extensions:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Extensions
{
/// <summary>
/// IEnumerable Utilities.
/// </summary>
public static partial class IEnumerableUtilities
{
/// <summary>
/// String.Join collection of items using custom Separator, String.Format and FormatProvider.
/// </summary>
public static string StringJoin<T>(this IEnumerable<T> Source)
{
return Source.StringJoin(" ", string.Empty, null);
}
/// <summary>
/// String.Join collection of items using custom Separator, String.Format and FormatProvider.
/// </summary>
public static string StrinJoin<T>(this IEnumerable<T> Source, string Separator)
{
return Source.StringJoin(Separator, string.Empty, null);
}
/// <summary>
/// String.Join collection of items using custom Separator, String.Format and FormatProvider.
/// </summary>
public static string StringJoin<T>(this IEnumerable<T> Source, string Separator, string StringFormat)
{
return Source.StringJoin(Separator, StringFormat, null);
}
/// <summary>
/// String.Join collection of items using custom Separator, String.Format and FormatProvider.
/// </summary>
public static string StringJoin<T>(this IEnumerable<T> Source, string Separator, IFormatProvider FormatProvider)
{
return Source.StringJoin(Separator, string.Empty, FormatProvider);
}
/// <summary>
/// String.Join collection of items using custom Separator, String.Format and FormatProvider.
/// </summary>
public static string StringJoin<T>(this IEnumerable<T> Source, IFormatProvider FormatProvider)
{
return Source.StringJoin(" ", string.Empty, FormatProvider);
}
/// <summary>
/// String.Join collection of items using custom Separator, String.Format and FormatProvider.
/// </summary>
public static string StringJoin<T>(this IEnumerable<T> Source, string Separator, string StringFormat, IFormatProvider FormatProvider)
{
//Validate Source
if (Source == null)
return string.Empty;
else if (Source.Count() == 0)
return string.Empty;
//Validate Separator
if (String.IsNullOrEmpty(Separator))
Separator = " ";
//Validate StringFormat
if (String.IsNullOrWhitespace(StringFormat))
StringFormat = "{0}";
//Validate FormatProvider
if (FormatProvider == null)
FormatProvider = CultureInfo.CurrentCulture;
//Convert items
var convertedItems = Source.Select(i => String.Format(FormatProvider, StringFormat, i));
//Return
return String.Join(Separator, convertedItems);
}
}
}