Most developers based on code I have seen separate strings like so:
for (int i=0; i<someData.Count; i++)
myStringBuilder.AppendFormat("{0},", someData[i]);
Then after constructing this string they need to remove the last separator
myStringBuilder = myStringBuilder.Remove(myStringBuilder.Length-1, 1);
I too have used this concept. However, there is another way to do this that a lot of people didn’t even know existed. The String.Join class; it takes 2 params, the separator and the array.
ArrayList<String> myList = new ArrayList<String>();
for (int i=0; i<someData.Count; i++)
myList.Add(someData[i]);
return String.Join(",", myList.ToArray());
This would return a string separated by a comma such as “Text,MoreText,EvenMore,AndLast”

1 comments:
Great tip - didn't know that about string.Join.
Post a Comment