흠을 사용하는 방법을 알고하지 않습니다, 귀하의 질문이 충분히 명확하지 않다 ... 그러나?
텍스트 파일의 모든 문자를 2 차원 배열에 매핑 하시겠습니까? 이 같은
뭔가 :
이
[0, 0] = "a"
[0, 1] = "b"
[0, 2] = "c"
..<omited>..
[4, 0] = "x"
...
and so on...
약간의 테스트 텍스트 파일 :
abcdefghij
1234567890
jihgfedcba
0987654321
xxxxxxxxxx
0000000000
yyyyyyyyyy
9999999999
----------
!!!!!!!!!!
C# 코드는 :
static void Main()
{
String input = File.ReadAllText(@"test.txt"); // read file content
input = input.Replace("\r\n", "\n"); // get rid of \r
int i = 0, j = 0;
string[,] result = new string[10,10]; // hardcoded for testing purposes
foreach (var row in input.Split('\n')) // loop through each row
{
j = 0;
foreach (var col in row.Select(c => c.ToString()).ToArray()) // split to array
{ // and loop through each char
result[i, j] = col; // Add the char to the jagged array => result
j++;
}
i++;
}
}
// EDIT: added some code to print out the result.
// Print all elements in the 2d array.
int rowLength = result.GetLength(0);
int colLength = result.GetLength(1);
for (int k = 0; k < rowLength; k++)
{
for (int h = 0; h < colLength; h++)
{
Console.Write("{0} ", result[k, h]);
}
Console.Write(Environment.NewLine + Environment.NewLine);
}
내가 배열의 크기를 하드 코딩했습니다 이 예제에서.
여기에 .txt 파일을 게시 할 수 있습니까? – Sandip
이것은 .txt 파일에있는 것의 예입니다. – user3223680
3 번 질문을 읽었으므로 정확히 무엇을 묻고 있는지 알 수 없습니다. – Dennisch