2/5/10

Generic Array to String Function

ArrayToString returns an array as a formatted string. This generic function works with arrays of all types.
//C#
using System.Text;

namespace ExpressionSoftware.System
{
    public static class Array
    {
        public static string ArrayToString<T>(T[] array, string format)
        {
            return ArrayToString(array, format, 16);
        }

        public static string ArrayToString<T>(T[] array, string format, int stringBuilderItemCapacity)
        {
            var sb = new StringBuilder(array.Length * stringBuilderItemCapacity);
            foreach (T item in array)
            {
                sb.AppendFormat(format, item);
            }
            return sb.ToString();
        }
    }
}

//F# v1.9.7.8
namespace ExpressionSoftware.System
open System.Text

type Array =
  
  //stringBuilderItemCapacity is an optional int parameter
  static member ArrayToString(array:'a[], format:string, ?stringBuilderItemCapacity:int) =
    
    let stringBuilderItemCapacity =
      match stringBuilderItemCapacity with
        | Some(stringBuilderItemCapacity) -> stringBuilderItemCapacity
        | None -> 16

    let sb = new StringBuilder(array.Length * stringBuilderItemCapacity)
    array |> Array.iter(fun b -> sb.AppendFormat(format, b) |> ignore)
    sb.ToString()

#PowerShell
function arrayToString($array, $format)
{
  $array | %{$result += ($format -f $_)}
  $result
}

Examples
//C#
byte[] bytes = { 0, 1, 255 };
int[] ints = { int.MinValue, -1, 0, 256, int.MaxValue };
float[] floats = { -9.99f, 0f, 3.14159f };
char[] chars = { 'a', 'b', 'c', '1', '2', '3' };
string[] strings = { "xyz", "789" };

Debug.WriteLine(Array.ArrayToString(bytes, "{0}", 3));
Debug.WriteLine(Array.ArrayToString(bytes, "{0} "));
Debug.WriteLine(Array.ArrayToString(ints, "{0} "));
Debug.WriteLine(Array.ArrayToString(floats, "{0} "));
Debug.WriteLine(Array.ArrayToString(chars, "{0}", 1));
Debug.WriteLine(Array.ArrayToString(chars, "{0},", 2));
Debug.WriteLine(Array.ArrayToString(strings, "{0} ", 4));

//F#
let bytes = [|0uy; 1uy; 255uy|]
let ints = [|Int32.MinValue; -1; 0; 256; Int32.MaxValue|]
let floats = [|-9.99f; 0.0f; 3.14159f|]
let chars = [|'a'; 'b'; 'c'; '1'; '2'; '3'|]
let strings = [|"xyz"; "789"|]

Array.ArrayToString(bytes, "{0}", 3) |> (fun s -> printfn "%s" s)
Array.ArrayToString(bytes, "{0} ") |> (fun s -> printfn "%s" s)
Array.ArrayToString(ints, "{0} ") |> (fun s -> printfn "%s" s)
Array.ArrayToString(floats, "{0} ") |> (fun s -> printfn "%s" s)
Array.ArrayToString(chars, "{0}", 1) |> (fun s -> printfn "%s" s)
Array.ArrayToString(chars, "{0},", 2) |> (fun s -> printfn "%s" s)
Array.ArrayToString(strings, "{0} ", 4) |> (fun s -> printfn "%s" s)

#PowerShell
[byte[]]$bytes = 0, 1, 255
[int[]] $ints = [int32]::MinValue, -1, 0, 256, [int32]::MaxValue
[single[]] $floats = -9.99, 0, 3.14159
[char[]] $chars = 'a', 'b', 'c', '1', '2', '3'
[string[]] $strings = 'xyz', '789'

arrayToString $bytes '{0}'
arrayToString $bytes '{0} '
arrayToString $ints '{0} '
arrayToString $floats '{0} '
arrayToString $chars '{0}'
arrayToString $chars '{0},'
arrayToString $strings '{0} '

Output
01255
0 1 255 
-2147483648 -1 0 256 2147483647 
-9.99 0 3.14159 
abc123
a,b,c,1,2,3,
xyz 789 


1 comment:

  1. .NET 4 string.Join() supports arrays and sequences
    string.Join(" - ", Enumerable.Range(1, 3))

    //output
    1 - 2 - 3

    jm

    ReplyDelete