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 ?")

5
  • 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. Commented Sep 11, 2014 at 7:19
  • Check out: stackoverflow.com/questions/6524528/… Commented Sep 11, 2014 at 7:23
  • For editing huge string: en.wikipedia.org/wiki/Rope_(data_structure). Maybe you will be able to find sufficient implementation. Commented Sep 11, 2014 at 7: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 Commented Sep 11, 2014 at 7:27
  • @Robban Sorry, it didn't show up in recommended questions. Yes, I think it's helpful. Commented Sep 11, 2014 at 7:48

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

1 Comment

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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.