我正嘗試讀取文本文件中的多行。它看起來像這樣:從文本文件中讀取多行
#############################################
B_ATTR 'MILLING_ORIGIN' 'MILLING 0 0.0 0' 3.635 0.405
B_ATTR 'DRILL_ORIGIN' '' 0.0 0.0
B_ATTR 'BOARD_ROUTING_OUTLINE' '' 0.0192 0.0885 0.0862 0.0215 3.84915 0.0215 -
3.9162 0.0885 3.9162 0.7018 3.8491 0.7689 3.6793 0.7689 3.65335 0.7715 3.6274 0.77935 -
3.60355 0.7921 3.5826 0.8093 3.5654 0.83025 3.55265 0.8541 3.5448 0.88 3.5422 0.906 -
3.5422 5.62345 3.54335 5.6408 3.54685 5.6585 3.55265 5.67565 3.56065 5.69185 3.57065 5.7069 -
3.58255 5.7205 3.59615 5.73245 3.61065 5.7421 3.9162 5.91855 3.9162 6.2136 3.8491 6.2807 -
0.0862 6.2807 0.0192 6.21365 0.0192 5.91855 0.32465 5.7421 0.33915 5.7324 0.3527 5.72045 -
0.36465 5.7069 0.3747 5.6919 0.3827 5.67565 0.3885 5.65855 0.39205 5.64085 0.3932 5.62345 -
0.3932 0.906 0.3906 0.88 0.38275 0.8541 0.37 0.83025 0.3528 0.8093 0.33185 0.7921 -
0.308 0.77935 0.28205 0.7715 0.2561 0.7689 0.0862 0.7689 0.0192 0.7018
B_ATTR 'BOARD_PLACEMENT_OUTLINE' '' 0.0192 0.0885 0.0862 0.0215 3.84915 0.0215 -
3.9162 0.0885 3.9162 0.7018 3.8491 0.7689 3.6793 0.7689 3.65335 0.7715 3.6274 0.77935 -
3.60355 0.7921 3.5826 0.8093 3.5654 0.83025 3.55265 0.8541 3.5448 0.88 3.5422 0.906 -
3.5422 5.62345 3.54335 5.6408 3.54685 5.6585 3.55265 5.67565 3.56065 5.69185 3
閱讀將與以「B_ATTR‘BOARD_ROUTING_OUTLINE’」,並結束與以「B_ATTR‘BOARD_PLACEMENT_OUTLINE’」啓動行啓動行啓動。我寫了這段代碼,但它不起作用。我無法擺脫一些空間。
StreamReader reader = new StreamReader("neutral.txt");
string lines = reader.ReadToEnd(); //Reading all lines.
//Takes the lines I want.
lines = lines.Substring(lines.IndexOf("B_ATTR 'BOARD_ROUTING_OUTLINE'"), lines.IndexOf("B_ATTR 'BOARD_PLACEMENT_OUTLINE'") - lines.IndexOf("B_ATTR 'BOARD_ROUTING_OUTLINE'"));
//if the line contains "-" , replaces with " "(space).
while (lines.Contains("-"))
lines = Regex.Replace(lines, @"\-", " ");
//if the line contains " "(two spaces), replaces with " "(one space).
while (lines.Contains(" "))
lines = Regex.Replace(lines, @"\ ", " ");
string[] items = lines.Split(' ');
List<string> list = new List<string>();
//Places all of the elements of items to list.
foreach (var element in items)
list.Add(element);
//if the list contains any space , removes it.
while (list.Contains(" "))
list.Remove(" ");
//Removes the elements I don't want.
list.Remove("B_ATTR");
list.Remove("'BOARD_ROUTING_OUTLINE'");
list.Remove("''");
list.Remove("B_ATTR");
list.Remove("'BOARD_PLACEMENT_OUTLINE'");
string[,] array = new string[50, 2];
string[] array2 = list.ToArray(); //Turns the list to array.
int counter = 0;
//Makes a rectangular array with 50 rows and 2 columns.
for (int a = 0; a < 50; a++)
for (int b = 0; b < 2; b++)
{
array[a, b] = array2[counter];
counter++;
}
我實際使用正則表達式來擺脫的空間,但我不能消除的線條和那些空間發生到陣列之間的空間。
你有什麼建議嗎?我做錯了嗎?
這會更容易使用[File.ReadLines](http://msdn.microsoft.com/en-us/library/system.io.file.readlines.aspx)或[File.ReadAllLines](http: //msdn.microsoft.com/en-us/library/system.io.file.readalllines.aspx) – lightbricko
使用File.ReadAllLines,您可以首先準備好陣列。您只需獲得兩個文本的「indexof」,並將其中的元素取出。 –
我試過了。我用File.ReadAllLines讀取它並使用「join」使它們成爲一行字符串。然後我使用「substring」並採取我想要的部分。但它顯示了我的錯誤。它說行太長,無法閱讀。 –