Yet Another Join Method
Posted by Ryan Baxter Fri, 12 Sep 2008 21:27:00 GMT
The .NET String type has a Join method, but in my latest ASP.NET project I had the need for joining String array elements with additional prefix and suffix values. The method below delimits array elements with the provided separator and concatenates the elements with the prefix and suffix string parameters.
I’ve found this particularly handy when creating SQL statements that require the IN keyword.
string[] names = { "Bobby", "Suzy" };
sql += "WHERE People.FirstName IN (" + Utility.Join(",", names, "'", "'") + ")";Add this method to your utility class or extended String type.
public static string Join(string separator, string[] value, string
prefix, string suffix)
{
string toReturn = String.Empty;
int i;
for (i = 0; i < value.Length; i++)
{
if (i != value.Length - 1)
toReturn += prefix + value[i] + suffix + separator;
else
toReturn += prefix + value[i] + suffix;
}
return toReturn;
}- Posted in Code Snippets
- Meta 1 comment, permalink, rss, atom
Comments
2 months later:

