M
M
MyOnAsSalat2017-10-16 09:58:51
C++ / C#
MyOnAsSalat, 2017-10-16 09:58:51

How to concatenate an array of int into 1 string C#?

It is necessary to combine the array of numbers into 1 line by gluing them in order and inserting a '+' symbol between them.
Can this be done without a single line loop using LINQ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nemiro, 2017-10-16
@AlekseyNemiro

// входящие данные
var arr = new int[] { 5, 10, 7, 123, 0 };

// Console.WriteLine(String.Join("+", arr.OrderBy(n => n).Select(n => n.ToString())));

// сортируем
var sorted = arr.OrderBy(n => n);

// склеиваем
Console.WriteLine(String.Join("+", sorted.Select(n => n.ToString())));

// или без вызова ToString(), если используемая версия .NET позволит:
// Console.WriteLine(String.Join("+", sorted));

// или с вызовом ToArray() для ранних версий .NET:
// String.Join("+", sorted.Select(n => n.ToString()).ToArray())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question