2013-03-08 71 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