简体   繁体   中英

Creating comma separated string from numeric values with multiple formats

I am creating a string with a list of comma separated values, but these numeric values have different formats. Currently I am using something like this:

var values = new StringBuilder();
// These are all of type double
values.AppendFormat("{0:F1},", aLength);
values.AppendFormat("{0:F4},", anAngle);
values.AppendFormat("{0:F1},", aHeight);
values.AppendFormat("{0:F2},{1:F2},", a2DPoint.X, a2DPoint.Y);
// while these are all of type int
values.Append(anID).Append(",");
values.Append(anInt).Append(",");
values.Append(anotherInt);
if (aListOfInt.Count > 0)
{
    values.Append(",");
    values.Append(String.Join(",", aListOfInt));
}

var resultString = values.ToString();

Is this the right way? I could do this also with String.Join , creating beforehand a List<string> similar to the StringBuilder values above, but would it better? Is there a more elegant way?

Note: I am new to C#

Try this:

var objList = new List<object>();
objList.Add(1);
objList.Add(2.0);
...
var resultString = string.Join(",", objList.Cast<string>());

Put your values in a list of objects, cast the values to string, and join the values.

But with the above you will loose your formatting. I have another solution for. I think this is a short syntax without loosing the format of each value:

var stringList = new List<string>{
    aLength.ToString("F1"),
    anAngle.ToString("F4"),
    ...
};
var resultString = string.Join(",", stringList);

If what you're looking for is more elegant (rather than optimal), you might try something like this:

//Note the commas in the formats,and that formats and data have the same size.
List<string> formats = new List<string>(new string[] {"{0:F1},", "{0:F4},", "{0:F2},",{0}});
List<object> data=new List<object>(new object[]{4,2,0.12,new object()});
StringBuilder resultBuilder=new StringBuilder();
for (int i = 0; i < 4; i++) {
       resultBuilder.AppendFormat(formats[i], data[i]);
 }
string result = resultBuilder.ToString();

As long as you're careful about how you set up your formats and data, you shouldn't have to do any other processing other than the for loop to "zip" them together. I'd suggest overriding ToString() on whatever class a2DPoint is, so you can shove it into a single format block rather than having special handling, or splitting up a2DPoint.X and a2DPoint.Y into separate data/format blocks. It's not the most efficient way to do it, though. The Lists take up room and time; Arrays would probably be better if you know ahead of time how many entries you'll have.

Edit: Example of method in comments Edit: Adding example of method referred to in comments:

public class FormatZipper {
    //This could be two lists, but it's probably more space/time efficent to use a single list. 
    List<Tuple<string,object>> _entries;
    StringBuilder _builder;
    public FormatZipper() {
        _builder = new StringBuilder();
        _entries = new List<Tuple<string, object>>();
    }
    public void AddEntry(string fmt, object data) {
        _entries.Add(Tuple.Create(fmt, data));
    }

    public string CurrentString {
        get {
            _builder.Clear();
            foreach (var entry in _entries) {
                _builder.AppendFormat(entry.Item1, entry.Item2);
            }
            return _builder.ToString();
        }
    }
}
class Program {
    static void Main(string[] args) {
        FormatZipper zipper = new FormatZipper();
        zipper.AddEntry("{0:F1},", 1);
        Console.WriteLine(zipper.CurrentString);
        zipper.AddEntry("{0:F2,", 1);
        Console.WriteLine(zipper.CurrentString);


    }
}

I think this does it quite nicely:

var resultString = String.Join(",", new []
{
    aLength.ToString("F1"),
    anAngle.ToString("F4"),
    aHeight.ToString("F1"),
    a2DPoint.X.ToString("F2"),
    a2DPoint.Y.ToString("F2"),
    anID.ToString(),
    anInt.ToString(),
    anotherInt.ToString(),
}.Concat(aListOfInt.Select(x => x.ToString())));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM