Lot of questions has been already asked about the differences between string and string builder and most of the people suggest that string builder is faster than string. I am curious to know if string builder is too good so why string is there? Moreover, can some body give me an example where string will be more usefull than string builder?
9 に答える
StringBuilder は、その名前が示すように、文字列を構築するために使用されますが、文字列は不変の文字値です。文字データを渡したい場合は文字列を使用し、文字データを操作したい場合は StringBuilder を使用します。
StringBuilder は、重いループや反復によって文字列を構築および変更する場合に適したオプションです。
いくつかの連結や単に文字列値を格納するなどの単純な文字列操作は、標準の文字列オブジェクトにはるかに適しています。
同様の質問?文字列と StringBuilder
どちらが有利かという話ではありません...
AString
はString
- 互いに隣接する 1 つ以上の文字です。何らかの方法で文字列を変更したい場合は、 immutableであるため、単純に文字列が作成されます。
AStringBuilder
は文字列を作成するクラスです。メモリ内に多くの冗長な文字列を作成することなく、それらを構築する手段を提供します。最終結果は常にString
.
これをしないでください
string s = "my string";
s += " is now a little longer";
また
s = s + " is now longer again";
これにより、メモリ内に5 つの文字列が作成されます (実際には、以下を参照してください)。
これを行う:
StringBuilder sb = new StringBuilder();
sb.Append("my string");
sb.Append(" is now a little longer");
sb.Append(" is now longer again");
string s = sb.ToString();
これにより、メモリ内に1 つの文字列が作成されます (以下を参照してください)。
あなたはこれを行うことができます:
string s = "It is now " + DateTime.Now + ".";
これにより、メモリ内に文字列が1つだけ作成されます。
補足として、 を作成するにStringBuilder
は、とにかく一定量のメモリが必要です。大まかな経験則として:
- ループ内で文字列を連結する場合は、常に a を使用し
StringBuilder
てください。 StringBuilder
文字列を 4 回以上連結する場合は、a を使用します。
値を変更する必要がある場合は、StringBuilder の方が適しています。そうでなければ、その必要はありません。
10箇所同じ文字列値をそのまま使う必要がある場合は、Stringを使用します。10 回変更する必要がある場合は、StringBuilder.
Would you really use a string builder here?
Console.WriteLine("MyString");
StringBuilder is great when you need to do large amounts of concatenation to build a string.
Jon Skeet has a great aricle on this here: http://www.yoda.arachsys.com/csharp/stringbuilder.html
Check out the section that starts "So I Should Use StringBuilder Everywhere, Right?"
He writes:
The important difference between this example and the previous one is that we can easily present all the strings which need to be concatenated together in one call to String.Concat. That means that no intermediate strings are needed. StringBuilder is efficient in the first example because it acts as a container for the intermediate result without having to copy that result each time - when there's no intermediate result anyway, it has no advantage.
StringBuilder は、文字列を作成する限り、文字列を使用するよりも高速ではありません。
StringBuilder は、(ループ内のように) 複数の文字列を連結する必要がある場合に高速になります。文字列ビルダーの内容を文字列に変換する必要がある場合、パフォーマンスが低下します。
文字列に対する StringBuilder の本当の利点の 1 つは、いくつかのメソッドを介して渡すことができ、途中で文字列を追加できることです。文字列オブジェクトを渡すことによってそれを行うことはできません。したがって、JSON オブジェクトなどのように構築する再帰操作がある場合は、StringBuilder を渡して値を追加できます。
文字列は不変であるため、優れています。そして、不変であることは良いことであり、関数型言語が証明していることです。
「これは文字列です」という文字列(への参照)がある場合、それが永遠に同じ値を持つことを確信できるからです。StringBuilder の値は (メソッド呼び出し、別のスレッドなどによって) 変更される可能性があります。
ただし、生の、純粋な速度とメモリ使用量の最適化が必要な場合があります。その場合、stringbuilder を使用します。
例:
var a = "This is a string";
var b = a;
a += ". And some part is added"; // a now references another string in memory
var aSB = new StringBuilder("This is a string");
var bSB = aSB
aSB.Append(". And some part is added"); // aSB and bSB still reference the same memory spot
Console.WriteLine(b); // ==> This is a string
Console.WriteLine(bSB); // ==> This is a string. And some part is added
どちらを使用するかの分析は、Concatenating Strings Efficientlyで提供されています。
基本的に、(完全な分析についてはリンクを読んでください)、StringBuilder
重要なループで連結するときに使用します。
簡単に言えば、可変と不変の間のトレードオフです
これを参照してください: StringBuilder と String