A
A
alex_ak12016-05-23 08:07:59
C++ / C#
alex_ak1, 2016-05-23 08:07:59

How to join from an array, after converting each element?

I have the following loop

r = "";
foreach( var t in arr )
r += "text=" + t + ",";

But as a result, an extra semicolon is formed at the end.
If there was no addition with "text=", then it would suit me. How can I catch the last element or make a join on the converted array? Of course, you can write at the end or there
String.Join( "text=", arr );
r = r.SubString( 0, r.Length-1 );
for( int i = 0; i < arr.Length; i++ )
r += "text" + t + (i == arr.Length-1 ? "" : "," );

But it's kind of long.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Larry Underwood, 2016-05-23
@alex_ak1

string[] arr = new[] { "1", "2" };
var result = string.Join(",", arr.Select(v => $"text={v}"));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question