In C#, how can I create a TextReader object from a string (without writing to disk)

后端 未结 6 441
悲哀的现实
悲哀的现实 2020-12-29 00:56

I\'m using A Fast CSV Reader to parse some pasted text into a webpage. The Fast CSV reader requires a TextReader object, and all I have is a string. What\'s the best way to

相关标签:
6条回答
  • 2020-12-29 01:06

    StringReader is a TextReader (StreamReader is too, but for reading from streams). So taking your first example and just using it to construct the CsvReader rather than trying to construct a StreamReader from it first gives:

    TextReader sr = new StringReader(TextBox_StartData.Text);
    using(CsvReader csv = new CsvReader(sr, true))
    {
      DetailsView1.DataSource = csv;
      DetailsView1.DataBind();
    }
    
    0 讨论(0)
  • 2020-12-29 01:14

    Use System.IO.StringReader :

    using(TextReader sr = new StringReader(yourstring))
    {
        DoSomethingWithATextReader(sr);
    }
    
    0 讨论(0)
  • 2020-12-29 01:19

    You want a StringReader

    var val = "test string";
    var textReader = new StringReader(val);
    
    0 讨论(0)
  • 2020-12-29 01:21

    Simply use the StringReader class. It inherits from TextReader.

    0 讨论(0)
  • 2020-12-29 01:30

    If you look at the documentation for TextReader, you will see two inheriting classes. And one of them is StringReader, which seems to do exactly what you want.

    0 讨论(0)
  • 2020-12-29 01:31

    Use the StringReader class, which inherits TextReader.

    0 讨论(0)
提交回复
热议问题