2013-03-08 36 views
0

當我使用StringReader.ReadLine來讀取字符串數組文本,它讀取它完美,但它同時會清除我的陣列,StringReader.ReadLine清除給定的字符串,是否有機會不清除字符串?

using (StringReader reader = new StringReader(filesGroupList[x])) 
     { 
      while ((filesGroupList[x] = reader.ReadLine()) != null) 
      { 
       ... 
      } 
     } 

現在,filesGroupList是空的。所以如果我想再次從這個字符串讀取數據,它會給我空引用異常,所以唯一的辦法是在使用ReadLine之前創建這個數組的副本,但是有機會避免它嗎?所以當StringReaded讀完這一行時,我的行仍然停留在數組中。

+1

我不明白什麼是你想acheive,它看起來非常奇怪 – nothrow 2013-03-08 16:46:36

+0

我想讀我的字符串具體的信息,但我會稍後再使用此字符串。 – inside 2013-03-08 16:49:35

回答

1

既然你所編寫的代碼,這裏是什麼,我看不出它的例子:

//going to set filesGroupList[x] to a string and then see what happens. 
filesGroupList[x] = "First line of string.\nSecond line of string.\n"; 
//now we go into the using portion. 
using (StringReader reader = new StringReader(filesGroupList[x])) 
{ 
    //reader has access to the whole string. 
    while ((filesGroupList[x] = reader.ReadLine()) != null) 
    { 
    //first time through, filesGroupList[x] is set to "First line of string." 
    //second time through, filesGroupLIst[x] is set to "Second line of string." 
    Console.WriteLine(filesGroupList[x]); 
    } 
    //third time through, ReadLine() returns null. 
    //filesGroupList[x] is set to null. 
    //Code breaks out of while loop. 
    Console.WriteLine(filesGroupList[x]); //outputs an empty line. 
} 
//outside of using, filesGroupList[x] still null. 
Console.WriteLine(filesGroupList[x]); //also outputs an empty line. 

現在,給我其他的答案,我建議使用線,我們就會把一切都相同,除了線部分。

//going to set filesGroupList[x] to a string and then see what happens. 
filesGroupList[x] = "First line of string.\nSecond line of string.\n"; 
using (StringReader reader = new StringReader(filesGroupList[x])) 
{ 
    //reader has access to the whole string. 
    string line; 
    while ((line = reader.ReadLine()) != null) 
    { 
    //first time through, line is set to "First line of string." 
    //second time through, line is set to "Second line of string." 
    Console.WriteLine(line); 
    } 
    //third time through, ReadLine() returns null. 
    //line is set to null. 
    //filesGroupList[x] is still set to "First line of string.\nSecond line of string.\n" 
    Console.WriteLine(line); //outputs an empty line. 
    Console.Write(filesGroupList[x]); //outputs whole string (on 2 lines). 
} 
Console.WriteLine(line); //won't compile. line doesn't exist. 
Console.Write(filesGroupList[x]); //outputs whole string (on 2 lines). 

所以我不認爲你想從filesGroupList[x]讀,然後存儲在filesGroupList[x]。如果filesGroupList[x]字符串沒有行尾字符,你乾脆把這個字符串右後衛它(然後把null在你下一次通過while循環)。如果filesGroupList[x]中的字符串確實有行尾字符,則每次通過while循環時,都會將字符串的一部分放回filesGroupList[x],我不認爲這是您的意圖。

0

編輯

實際上using語句字符串爲空後,預計時間:

StringReader.Dispose:釋放的TextReader對象使用的所有資源。 (繼承自TextReader的。)

MSDN Link here

的問題是,爲什麼要做到這一點,它不似乎是合理的,你想要做什麼。沒有更多的上下文信息。


您的代碼功能完美,或者你無法解釋自己,或者你的問題是完全不相關的東西。

我只是想自己:

static void Main(string[] args) 
{ 
    string[] filesGroupList = new string[1]; 

    int x = 0; 

    filesGroupList[x] = "String is here!"; 

    using (StringReader reader = new StringReader(filesGroupList[x])) 
    { 
     while ((filesGroupList[x] = reader.ReadLine()) != null) 
     { 
      Console.WriteLine("the string is here, I just checked"); 
      Console.WriteLine(filesGroupList[x]); 
     } 
    } 
    Console.ReadLine(); 
} 

和輸出:

字符串是在這裏,我只是檢查

字符串就在這裏!

+0

是的,但嘗試把Console.WriteLine(filesGroupList [x]); using語句 – inside 2013-03-08 16:55:35

+0

後,我以爲你會用它在......地方,看到編輯 – RMalke 2013-03-08 17:08:34

+0

我想我解釋我爲什麼要這麼做:我會稍後再使用此字符串。 – inside 2013-03-08 17:34:09

-1

,我認爲你的錯誤就在這裏:

while ((filesGroupList[x] = reader.ReadLine()) != null) 

您正在改變的filesGroupList[x]每次讀取時間和上次值,將其設置爲null

相反,如果你做了這樣的事情:

string line; 
while ((line = reader.ReadLine()) != null) 
... 

然後,一旦你是使用之外,你會發現filesGroupList[x]不變。

+0

然後我不讀我的filesGroupList,但我正在閱讀線,這是空的。 – inside 2013-03-08 21:01:59

+0

如果讀取器建立的相同方式'新StringReader(filesGroupList [X])'然後線從filesGroupList設置於各行[X]。從filesGroupList [x]讀取所有行後,它只會是空的。 – 2013-03-08 21:07:57