반응형
구분 된 문자열을 List로 split ()하는 방법
나는이 코드를 가지고 있었다 :
String[] lineElements;
. . .
try
{
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
lineElements = line.Split(',');
. . .
그러나 대신 List와 함께 가야한다고 생각했습니다. 그러나이 코드 :
List<String> listStrLineElements;
. . .
try
{
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
listStrLineElements = line.Split(',');
. . .
... " 'string []'형식을 'System.Collections.Generic.List'로 암시 적으로 변환 할 수 없습니다. "
string.Split()
배열을 반환합니다. 다음을 사용하여 목록으로 변환 할 수 있습니다 ToList()
.
listStrLineElements = line.Split(',').ToList();
.ToList () 함수에 액세스하려면 System.Linq를 가져와야합니다.
어느 쪽이든 사용하십시오 :
List<string> list = new List<string>(array);
또는 LINQ에서 :
List<string> list = array.ToList();
또는 특정 구현에 의존하지 않도록 코드를 변경하십시오.
IList<string> list = array; // string[] implements IList<string>
네임 스페이스를 사용하여 포함 System.Linq
List<string> stringList = line.Split(',').ToList();
각 항목을 반복하여 쉽게 사용할 수 있습니다.
foreach(string str in stringList)
{
}
String.Split()
배열을 반환하므로 다음을 사용하여 목록으로 변환하십시오. ToList()
그냥 당신과 함께 사용할 수 있습니다 using System.Linq;
List<string> stringList = line.Split(',') // this is array
.ToList(); // this is a list which you can loop in all split string
이 List 시도하십시오 stringList = line.Split ( ','). ToList ();
이것은 csv 파일을 읽고 큰 따옴표를 처리하는 csv 라인 스플리터를 포함하며 Excel에서 열었더라도 읽을 수 있습니다.
public List<Dictionary<string, string>> LoadCsvAsDictionary(string path)
{
var result = new List<Dictionary<string, string>>();
var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
System.IO.StreamReader file = new System.IO.StreamReader(fs);
string line;
int n = 0;
List<string> columns = null;
while ((line = file.ReadLine()) != null)
{
var values = SplitCsv(line);
if (n == 0)
{
columns = values;
}
else
{
var dict = new Dictionary<string, string>();
for (int i = 0; i < columns.Count; i++)
if (i < values.Count)
dict.Add(columns[i], values[i]);
result.Add(dict);
}
n++;
}
file.Close();
return result;
}
private List<string> SplitCsv(string csv)
{
var values = new List<string>();
int last = -1;
bool inQuotes = false;
int n = 0;
while (n < csv.Length)
{
switch (csv[n])
{
case '"':
inQuotes = !inQuotes;
break;
case ',':
if (!inQuotes)
{
values.Add(csv.Substring(last + 1, (n - last)).Trim(' ', ','));
last = n;
}
break;
}
n++;
}
if (last != csv.Length - 1)
values.Add(csv.Substring(last + 1).Trim());
return values;
}
string[] thisArray = myString.Split('/');//<string1/string2/string3/--->
List<string> myList = new List<string>(); //make a new string list
myList.AddRange(thisArray);
문자열 목록 AddRange
을 전달 string[]
하고 얻는 데 사용 합니다.
참고 URL : https://stackoverflow.com/questions/9263695/how-to-split-a-delimited-string-to-a-liststring
반응형
'IT박스' 카테고리의 다른 글
파이썬의 "in"세트 연산자 (0) | 2020.07.17 |
---|---|
jQuery $ .getScript () 메소드를 사용하여 여러 JS 파일을 포함하는 방법 (0) | 2020.07.16 |
반응 네이티브에서 컴포넌트 숨기기 / 보이기 (0) | 2020.07.16 |
URL에 대한 PHP 유효성 검사 / 정규식 (0) | 2020.07.16 |
AngularJS로 배열을 필터링하고 필터링 된 객체의 속성을 ng-model 속성으로 사용하려면 어떻게합니까? (0) | 2020.07.16 |