どうも、ちょげ(@chogetarou)です。
文字列(string)をカンマ区切りで分割したリストに変換する方法を紹介します。
方法

文字列(string)をカンマ区切りで分割したリストに変換するには、Split()とToList()を使います。
まず、System.Linqを導入します。
using System.Linq;
次に、文字列からSplit()を呼び出します。
Split()の引数にカンマの文字列を指定します。
そして、Split()からToList()を呼び出します。
//text=対象の文字列
List<string> result = text.Split(',').ToList();
上記のSplit()とToList()は、文字列をカンマ区切りで分割したリストに変換します。
使用例
using System;
using System.Collections.Generic;
using System.Linq;
public class Example
{
public static void Main()
{
string text = "ade,fg,hi,j,k";
List<string> result = text.Split(',').ToList();
foreach(string item in result)
{
Console.WriteLine(item);
}
}
}
出力:
abc
de
fg
hi
j
k
コメント