concatenate string, with different first and last iteration Good day. My code works but I think there is a better way to do this. Like string pattern or string manipulation. I'm not yet familiar with both terms.
The goal is to get &quot;=A1-A2-B3-D4-WHATEVER STRING=&quot;. From array of strings.
The code is :
string[] arr = { &quot;A1&quot;, &quot;A2&quot;, &quot;B3&quot;, &quot;D4&quot;, &quot;WHATEVER STRING&quot;}; // can be any string value
string newString = &quot;=&quot;;
for (int i = 0; i &lt; arr.Length; i++) {
    if (i == arr.Length-1) {
        newString  += arr[i].ToString() + &quot;=&quot;; 
    } else {
        newString += arr[i].ToString() + &quot;-&quot;;
    }
}
You should use String.Join() here. Its easier to read and shorter as well.
Like
string[] arr = { &quot;A1&quot;, &quot;A2&quot;, &quot;B3&quot;, &quot;D4&quot;, &quot;WHATEVER STRING&quot; }; // can be any string value
string result = &quot;=&quot; + string.Join(&quot;-&quot;, arr) + &quot;=&quot;;