どうも、ちょげ(@chogetarou)です。
Dictionary(連想配列)のキー(Key)を連結して、1つの文字列にする方法を紹介します。
方法

Dictionary(連想配列)のキー(Key)を連結して文字列に変換するには、String.Joinを使います。
まず、String.Join()を呼び出します。
そして、String.Joinの第1引数に区切り文字、第2引数にDictionaryのKeysプロパティを指定します。
string keys = String.Join(",", myDict.Keys);
上記のString.Join()は、第2引数のKeysプロパティにアクセスしたDictionaryのキーを結合した文字列を返します。
また、結合した文字列では、1つ1つの値がString.Join()の第1引数に指定した文字で区切られます。
使用例
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
Dictionary<string, int> numbers = new Dictionary<string, int>()
{
{ "one", 1 },
{ "two", 2 },
{ "three", 3 },
{ "four", 4 },
{ "five", 5 },
};
string keys = String.Join(",", numbers.Keys);
Console.WriteLine(keys); //one,two,three,four,five
}
}
コメント