Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping over a string - Is there a better way?

I am passing a String Array to a function with the code below. The strings in the array are the first part of email addresses. I need to add domain.com to the end of each string and a "," between each address. I have working code below, but just wondering if there is a (better/cleaner/efficient) way of doing this?

String toAddress = "";
for (int x = 0; x < addresses.Length; x++)
{
    if (x == (addresses.Length-1))
    {
        toAddress += addresses[x] + "@domain.com";
    }
    else
    {
        toAddress += addresses[x] + "@domain.com,";
    }
}
like image 611
SANM2009 Avatar asked Dec 10 '22 08:12

SANM2009


1 Answers

You could use Join and Linq Select to solve this

string toAddress = string.Join(",", addresses.Select(x => x + "@domain.com"));
like image 150
fubo Avatar answered Dec 19 '22 11:12

fubo