What is the fastest way of converting an array of floats to string?

11,815

Solution 1

I would go for the most readable string.Join which also should have sufficient performance in most cases. Unless there is a real issue, I would not run my own:

float[] values = { 1.0f, 2.0f, 3.0f };
string s = string.Join(" ", values);

It might be that I misread your question, so in case you want an enumeration of string go with the other answers.

Solution 2

To be more explicit, call float.ToString() manually and then string.Join() to separate each result with a space:

var array = new float[] { 0.1, 1.1, 1.0, 0.2 };
string result = String.Join(" ", array.Select(f => f.ToString(CultureInfo.CurrentCulture));

btw,

in .NET 2.0/3.0/3.5 there only single String.Join(string, string[])

but in .NET 4.0 there is also String.Join<T>(string, IEnumerable<T>)

@0xA3 uses method from .NET 4.0. Mine too. So for earlier versions use array.Select(..).ToArray()

Solution 3

float[] arr = { 1.0f, 2.1f };
var str = arr.Select(x => x.ToString()).ToArray();

or use rray.ConvertAll

public static string FloatFToString(float f)
{
    return f.ToString(); 
}

float[] a = { 1.0f, 2.1f };
var res = Array.ConvertAll(a, new Converter<float, string>(FloatFToString));

Solution 4

You can do it like this:

var floatsAsString = yourFloatArray.Select(f => f.ToString(CultureInfo.CurrentCulture));
Share:
11,815
Admin
Author by

Admin

Updated on July 21, 2022

Comments

  • Admin
    Admin almost 2 years

    What is the fastest way of converting an array of floats into string in C#?

    If my array contains this { 0.1, 1.1, 1.0, 0.2 }

    Then I want each entry to converted to a string with value separated by a white space, i.e. "0.1 1.1 1.0 0.2"

  • abatishchev
    abatishchev over 13 years
    Probably, it will just call float.ToString() for each element in the array
  • Admin
    Admin over 13 years
    Isn't compiling. Says "Join" has some invalid arguments.
  • Dirk Vollmar
    Dirk Vollmar over 13 years
    @abatishchev: Yes, it will, and then it uses a string builder to concatenate the elements. If you need specific formatting then your answer offers all possibilities.
  • Dirk Vollmar
    Dirk Vollmar over 13 years
    @Wajih: Sorry, I forgot to check it, but my solution only works with .NET 4.0.
  • Admin
    Admin over 13 years
    Dude, you're the man, thanks indeed!!!
  • C.J.
    C.J. over 13 years
    this answer is readable. The 'checked' answer is not.
  • abatishchev
    abatishchev over 13 years
    Using String.Concat and String.Format definitely is very inefficient
  • LukeH
    LukeH over 13 years
    @Wajih: If you need it to work in older versions of .NET then just do string s = string.Join(" ", values.Select(f => f.ToString()).ToArray())
  • iburlakov
    iburlakov over 13 years
    @abatishchev So, what is better way?
  • Dirk Vollmar
    Dirk Vollmar over 13 years
    This seems to be the most complete answer.
  • abatishchev
    abatishchev over 13 years
    Mine or @0xA3's way, I guess. Because String.Join uses StringBuilder internally that's much more efficient because of strings immutability in .NET.
  • iburlakov
    iburlakov over 13 years
    @abatishchev Thanks for info, i didn't know that String.Join uses StringBuilder internally.
  • abatishchev
    abatishchev over 13 years
    You can investigate such things using RedGate .NET Reflector
  • abatishchev
    abatishchev over 13 years
    Also as far as Array has not its own method Aggregate there is no need to call AsEnumerable because Array is already IEnumerable