StringReader.ReadLine清除给定的字符串,是否有可能不清除字符串

本文关键字:字符串 清除 是否 有可能 ReadLine StringReader | 更新日期: 2023-09-27 18:02:53

当我使用StringReader。ReadLine从字符串数组中读取文本,它读得很好但也会清空数组

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

现在,filesGroupList为空。所以,如果我想再次从这个字符串中读取数据,它会给我null引用异常,所以我唯一的方法是在使用ReadLine之前创建这个数组的副本,但是有机会避免它吗?因此,当stringread完成读取行,我的行仍然留在数组内。

StringReader.ReadLine清除给定的字符串,是否有可能不清除字符串

给定您编写的代码,下面是我看到的示例:

//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.

现在,考虑到我建议使用line的另一个答案,我们将保持所有内容不变,除了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]中的字符串没有行尾字符,您只需将该字符串放回其中(然后在下次通过while循环将null放入)。如果filesGroupList[x]中的字符串确实有行结束字符,那么每次通过while循环,您都将字符串的一部分放回filesGroupList[x],我认为这不是您的意图。

编辑

确实在using语句之后字符串是空的,因为:

StringReader。释放TextReader对象使用的所有资源。(继承自TextReader)

MSDN链接到这里

问题是为什么你想做的事情似乎不合理。没有更多的上下文信息。


你的代码功能完美,或者你无法解释自己,或者你的问题是在完全无关的东西。

I just try myself:

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();
}

输出:

字符串在这里,我刚刚检查了

字符串在这里!

我认为你的错误就在这里:

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

每次读取时都更改filesGroupList[x]的值,最后一次将其设置为null

如果相反,你做了这样的事情:

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

然后,一旦你在使用之外,你会发现filesGroupList[x]没有改变。