Sometimes you will have an array of string that you need to concatenate in a string with delimiter. The static Join method of the string class can easily join the array of strings in as little as one line of code. For example:
string[] infoArray = new string[5] {"I", "want", "to be", "financial", "freedom"}; string delimitedInfo = string.Join(",", infoArray);
This code sets the value of delimitedInfo to the following:
I,want,to be,financial,freedom
The Join method concatenates all the strings contained in a string array. Additionally, a specified delimiting character(s) is inserted between each string in the array. This method returns a single string object with the fully joined and delimited text.
Unlike the Split method of the string class, the Join method accepts only one delimiting character at a time. In order to use multiple delimiting characters within a string of values, subsequent Join operations must be performed on the information until all of the data has been joined together into a single string. For example:
string[] infoArray = new string[4] {"12", "12", "Checking", "Savings"}; string delimitedInfoBegin = string.Join(",", infoArray, 0, 2); string delimitedInfoEnd = string.Join(",", infoArray, 2, 2); string[] delimitedInfoTotal = new string[2] {delimitedInfoBegin, delimitedInfoEnd}; string delimitedInfoFinal = string.Join(":", delimitedInfoTotal); Console.WriteLine(delimitedInfoFinal);
produces the following delimited file:
11,12:Checking,Savings
Popularity: 1% [?]
RSS feed for comments on this post · TrackBack URI
Leave a reply