C #: how to convert List <string> to a string connected with a separator ?,
I often use a knowledge point. I went to Baidu every time I used it, and today I used it again (I ran to ferry again). To avoid forgetting it when I used it, I searched it and recorded it today.
How to combine the List <string> generic set into a string Based on the delimiter (such as a comma? In the past, we used loops to concatenate strings. Not only do we need to write more code, but it also consumes more system resources. GenerallyString. Join (string separator, IEnumerable <T> values)This method is used to combine a set into a string using separators.
Here is the complete description of the string. Join method:
//
// Summary:
// System. Collections. Generic. IEnumerable <T> of the series type System. String is used to construct the members of the set. The specified separator is used between each member.
//
// Parameters:
// Separator:
// The string to be used as the separator.
//
// Values:
// A set of strings to be concatenated.
//
// Return result:
// A string composed of values members, which are separated by the separator string.
//
// Exception:
// System. ArgumentNullException:
// Values is null.
[ComVisible (false)]
Public static string Join (string separator, IEnumerable <string> values );
The following code example:
List <string> testList = new List <string> {"Jim", "John", "Linda", "Sam "};
String result = string. Join (",", testList );
Console. WriteLine ("These People are your partner:" + result );
Output result: