StringStream in C#

I want to create Stream for build string. I want write code like:

void Print(Stream stream) {
// some code with stream
}

void Main() {
  StringStream stream = new StringStream();
  Print(stream);
  string myString = stream.GetResult();
}

How I can do it?

Upd: I have method Print in external dll. I need to use Stream, before Stream is args of method Print. But I want have result of method Print as string.

Initializing a stringstream c++

Using c++, i needed to concatenate a string with integers, to do that i used stringstream in the following way: int numPeople = 10; stringstream ss; ss << Number of people is << numPeop

C++ stringstream tellg()

I’m writing a little parser in c++98 (yup, cannot use 11). I’m working with a std::stringstream which I pass by reference to different functions, let’s call them subparsers. In order to know which sub

C++ stringstream concatenate query

The code is supposed to concatenate argv[1] with .txt , and with _r.txt . std::stringstream sstm; std::stringstream sstm_r; sstm<<argv[1]<<.txt; sstm_r<<argv[1]<<_r.txt; c

C++ stringstream inline

I would like to use std::stringstream to create formatted strings, but use the class inline so I dont have stringstream local variables flying around. What I mean is this: #include <iostream> #i

C++ stringstream

How does stringstream work when you do this: stringstream ss; ss << 123 << abc; Does it create a throwaway 123abc string or does it do both operations consecutively to the stringstr

C++ – stringstream << “overwriting”

I’m making an OpenGL game in C++. I’m fairly inexperinced in C++ as opposed to other languages. Anyway, I create a stringstream with the base directory for some images. I then pass this stringstream

stringstream errors c++

I am trying to understand how stringstream works in order to be able to identify and convert possible numbers that were inputted as strings… for some reason this small piece of code I wrote to try a

bada C++ std::stringstream

bada crashed on stringstream read. json::Object objDocument = d(); std::stringstream stream; json::Writer::Write(objDocument, stream); json::Object objDocument2; json::Reader::Read(objDocument2, strea

C++ stringstream returning extra character?

I’ve been attempting to use the C++ stringstream class to do some relatively simple string manipulations, but I’m having a problem with the get() method. For some reason whenever I extract the output

Passing python StringIO to C++ stringstream using swig

I’ve got some python code that generates a StringIO variable. I’d like to pass this variable to a C++ function using a stringstream parameter (on the assumption C++ stringstream is the closest match t

Answers

You can use a StringWriter to write values to a string. It provides a stream-like syntax (though does not derive from Stream) which works with an underlying StringBuilder.

Since your Print() method presumably deals with Text data, could you rewrite it to accpet a TextWriter parameter?

The library provides a StringWriter: TextWriter but not a StringStream. I suppose you could create one by wrapping a MemoryStream, but is it really necessary?


After the Update:

void Main() 
{
  string myString;  // outside using

  using (MemoryStream stream = new MemoryStream ())
  {
     Print(stream);
     myString = Encoding.UTF8.GetString(stream.ToArray());
  }
  ... 

}

You may want to change UTF8 to ASCII, depending on the encoding used by Print()

You have a number of options:

One is to not use streams, but use the TextWriter

   void Print(TextWriter writer) 
   {
   }

   void Main() 
  {
    var textWriter = new StringWriter();
    Print(writer);
    string myString = textWriter.ToString();
   }

It’s likely that TextWriter is the appropriate level of abstraction for your print function. Streams are aimed at writing binary data, while TextWriter works at a higher abstraction level, specifically geared towards outputting strings.

If your motivation is that you also want your Print function to write to files, you can get a text writer from a filestream as well.

void Print(TextWriter writer) 
{
}

void PrintToFile(string filePath) 
{
     using(var textWriter = new StreamWriter(filePath))
     {
         Print(writer);
     }
}

If you REALLY want a stream you can look at MemoryStream.

You can use tandem of MemoryStream and StreamReader classes:

void Main()
{
    string myString;

    using (var stream = new MemoryStream())
    {
        Print(stream);

        stream.Position = 0;
        using (var reader = new StreamReader(stream))
        {
            myString = reader.ReadToEnd();
        }
    }
}