どうも、ちょげ(@chogetarou)です。
List(リスト)の文字列の要素をカンマ区切りで結合して1つの文字列にする方法を紹介します。
方法

List(リスト)の文字列の要素をカンマ区切りで結合するには、String.Join()を使います。
まず、String.Join()を呼び出します。
そして、String.Join()の第1引数に「”,”」、第2引数にListを指定します。
string result = String.Join(",", list);
上記のString.Join()は、Listの文字列の要素をカンマ区切りで結合した文字列を返します。
使用例
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
List<string> numbers = new List<string>() { "1", "2", "3", "4", "5" };
string strNums = String.Join(",", numbers);
Console.WriteLine(strNums); //1,2,3,4,5
}
}
コメント