質問

何が最善の方法で書くStringBuilderます。IO.ストリーム?

ア:

StringBuilder message = new StringBuilder("All your base");
message.Append(" are belong to us");

System.IO.MemoryStream stream = new System.IO.MemoryStream();
System.Text.ASCIIEncoding encoding = new ASCIIEncoding();
stream.Write(encoder.GetBytes(message.ToString()), 0, message.Length);
役に立ちましたか?

解決

なStringBuilderであれば、書面による、ストリームいっぱいで仕分けもバッチリで、 StreamWriter:

using (var memoryStream = new MemoryStream())
using (var writer = new StreamWriter(memoryStream ))
{
    // Various for loops etc as necessary that will ultimately do this:
    writer.Write(...);
}

他のヒント

これは最善の方法です。他の賢明な損失はStringBuilderのとは、以下のようなものを使用します:

using (MemoryStream ms = new MemoryStream())
{
    using (StreamWriter sw = new StreamWriter(ms, Encoding.Unicode))
    {
        sw.WriteLine("dirty world.");
    }
    //do somthing with ms
}

おそらくそれは便利になります。

var sb= new StringBuilder("All your money");
sb.Append(" are belong to us, dude.");
var myString = sb.ToString();
var myByteArray = System.Text.Encoding.UTF8.GetBytes(myString);
var ms = new MemoryStream(myByteArray);
// Do what you need with MemoryStream

あなたのユースケースに応じて、それはまた、単ににStringWriterを開始する意味があります:

StringBuilder sb = null;

// StringWriter - a TextWriter backed by a StringBuilder
using (var writer = new StringWriter())
{
    writer.WriteLine("Blah");
    . . .
    sb = writer.GetStringBuilder(); // Get the backing StringBuilder out
}

// Do whatever you want with the StringBuilder

あなたはそれがきれいなので周りに渡すとで動作するようにStringBuilderのようなものを使用したい場合、あなたは、私が作成した以下のStringBuilderの代替のようなものを使用することができます。

それは違うん最も重要なことは、それが最初の文字列またはByteArrayのにそれを組み立てることなく、内部データへのアクセスを可能にすることです。これは、メモリ要件を倍増し、あなたの全体のオブジェクトに合うメモリの連続チャンクを割り当てようリスクする必要がないことを意味します。

注:私は、その後、内部List<string>()を使用しますが、これは簡単だったし、私の目的のために十分であることが証明され、より良いオプションがあると確信しています。

public class StringBuilderEx
{
    List<string> data = new List<string>();
    public void Append(string input)
    {
        data.Add(input);
    }
    public void AppendLine(string input)
    {
        data.Add(input + "\n");
    }
    public void AppendLine()
    {
        data.Add("\n");
    }

    /// <summary>
    /// Copies all data to a String.
    /// Warning: Will fail with an OutOfMemoryException if the data is too
    /// large to fit into a single contiguous string.
    /// </summary>
    public override string ToString()
    {
        return String.Join("", data);
    }

    /// <summary>
    /// Process Each section of the data in place.   This avoids the
    /// memory pressure of exporting everything to another contiguous
    /// block of memory before processing.
    /// </summary>
    public void ForEach(Action<string> processData)
    {
        foreach (string item in data)
            processData(item);
    }
}

今、あなたは、次のコードを使用してファイルに全体の内容をダンプすることができます。

var stringData = new StringBuilderEx();
stringData.Append("Add lots of data");

using (StreamWriter file = new System.IO.StreamWriter(localFilename))
{
    stringData.ForEach((data) =>
    {
        file.Write(data);
    });
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top