Wednesday, April 23, 2008

CSharp (c#) method String.Join().

When we should create a string, which consists of array of strings with a separator usually we use a iterator (for, foreach).

For example,

string[] stringData = new string[] {"one", "two", "three"};
string outputContent1 = "";
foreach (string item in stringData)
{
outputContent1 += item + ";";
}
outputContent1 = outputContent1.Remove(outputContent1.Length - 1);
Debug.Print("output #1: {0}", outputContent1);



Now, we can use csharp (c#) method String.Join:



string outputContent2 = String.Join(";", stringData);
Debug.Print("output #2: {0}", outputContent2);



Output window:



joinOutput



Reference:



String.Join Method

No comments: