2016-07-25 57 views
-3

我有一個文本文件,內容爲:如何提取2線之間線在C#

************** 

Some text 

************** 

我想讀在C#****之間的文本。 我怎樣才能達到同樣的升C

+0

讀每一行,尋找線= *****」,過程下一行,看着每一個,直到你看到‘****’再次 – pm100

+0

我的意思是,RLY,寫一些代碼 – pm100

回答

0

下面是如何讀取文本文件的行一般:What's the fastest way to read a text file line-by-line?

你可以這樣做:

var lines = File.ReadAllLines(fileName); 
int numLines = lines.Length; 
for (int lineCounter = 1 /*discard the first*/;lineCounter<Length-1 /*discard the last*/;lineCounter++) 

{ 
    Do whatever you want with lines[lineCounter] 
} 
1

你可以使用ReadAllText得到該文件的內容,然後ReplaceTrim刪除該文件的內容無用:

var result = System.IO.File.ReadAllText(@"c:\path\to\file.txt") 
    .Replace("*", string.Empty) // remove the asterisks 
    .Trim(); // trim whitespace and newlines 
-1

如果你知道****計數,那麼這可能會有所幫助。

string readContents; 
using (StreamReader streamReader = new StreamReader(path, Encoding.UTF8)) 
{ 
    readContents = streamReader.ReadToEnd(); 
    int start = readContents.IndexOf("*****") + 1; 
    int end = readContents.LastIndexOf("*****", start); 
    string result = readContents.Substring(start, end - start); 

} 
+0

這會還讀了*******行 –

+0

@MartinBrown代碼更新了。請檢查 –

+0

不行,仍然不起作用。假設文件中包含「***** \ r \ nSome Text \ r \ n *** **「。變量'start'最終爲1. LastIndex然後在結尾爲1的字符串中查找」*****「,即它在」*「中查找」*****「。'結束「然後變成-1使子字符串失敗。 –