1

I have a string that I have to edit quite a lot. Length is undefined. Replace(string, string) will be the most used method for this.

What is better string.Replace("", "") or StringBuilder.Replace("", "") ?

public static string FormatMyString(string input)
{
    // ...
}

(The code examples are plain and stupid. They just serve the purpose to show you what I'm doing. I always get questions: "What are you trying to do ?")

Bitterblue
  • 13,162
  • 17
  • 86
  • 124
  • 3
    Who knows what's best? You need to define your criteria for "better" and then you have to benchmark **your code with your data**. Only then will you know. – Enigmativity Sep 11 '14 at 07:19
  • Check out: http://stackoverflow.com/questions/6524528/string-replace-vs-stringbuilder-replace – Robban Sep 11 '14 at 07:23
  • For editing huge string: http://en.wikipedia.org/wiki/Rope_(data_structure). Maybe you will be able to find sufficient implementation. –  Sep 11 '14 at 07:25
  • String.replace("","") creates new string after replacement. As per your criteria string length is undefined so consider memory stringbuilder is better then string.Because stringbuilder makes changes in existing string and doesn't create new string. Both are not thread safe.so we can't compare the speed of execution between string and string builder – Selva Sep 11 '14 at 07:27
  • @Robban Sorry, it didn't show up in recommended questions. Yes, I think it's helpful. – Bitterblue Sep 11 '14 at 07:48

3 Answers3

2

What is better string.Replace("", "") or StringBuilder.Replace("", "") ?

Neither. They both do nothing useful. In the more general case:

  • if you're doing one replacement, the internal-call of String.Replace should be fine
  • if you're doing lots of replacements, consider StringBuilder to avoid intermediate strings
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    But it still comes down to actually benchmarking the code to know what's best. And you have to compare memory usage, speed of execution, and also code maintainability to make the best choice. – Enigmativity Sep 11 '14 at 07:36
0

IMHO String Builder is the way to go.

It has a cost for creating but would offer much better performence in general due to much more efficient string manipulation and concatenation.

As I said there is a cost so you should consider what does "edit quite a lot" mean.

Sorry I can't provide actual benchmark results right now, but I assume the threshold for using String Builder should be very low...

Hope this helps.

  • It is not necessarily true that `StringBuilder` will perform faster. I just tested building 10,000,000 strings using `StringBuilder` versus string concatenation and `StringBuilder` was twice as **slow**. You **have** to benchmark to make statements like this. – Enigmativity Sep 11 '14 at 07:34
0

You just don't get any good value just "asking" about these kinds of things. You need to benchmark. Take this code for example:

var sw = Stopwatch.StartNew();
var cc0 = GC.CollectionCount(0);
var s = (string)null;
for (var i = 0; i < 10000000; i++)
{
    s = "a";
    s += "b";
}
var cc1 = GC.CollectionCount(0);
sw.Stop();
Console.WriteLine(
    "collections == {0}, ms == {1}, string == \"{2}\"",
    cc1 - cc0,
    sw.ElapsedMilliseconds,
    s);

Versus this code:

var sw = Stopwatch.StartNew();
var cc0 = GC.CollectionCount(0);
var sb = (StringBuilder)null;
for (var i = 0; i < 10000000; i++)
{
    sb = new StringBuilder();
    sb.Append("a");
    sb.Append("b");
}
var cc1 = GC.CollectionCount(0);
Console.WriteLine(
    "collections == {0}, ms == {1}, string == \"{2}\"",
    cc1 - cc0,
    sw.ElapsedMilliseconds,
    sb.ToString());

The two results I get are:

collections == 63, ms == 336, string == "ab" // +=
collections == 228, ms == 692, string == "ab" // StringBuilder

The StringBuilder takes over twice as long and causes over 3.5 times more garbage collections to occur.

It's certainly the case that if I were to concatenate very long strings that StringBuilder will perform better, but I won't know that point unless I measure it.

You need to provide more detail as to what code you are running and what you mean by "better" (faster, less memory, easy to read code, etc) before we can say what is best.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172