Guy
Guy

Reputation: 67260

Indexing into a String in C# as an L-Value

I have the following intentionally trivial function:

void ReplaceSome(ref string text)
{
    StringBuilder sb = new StringBuilder(text);
    sb[5] = 'a';
    text = sb.ToString();
}

It appears to be inefficient to convert this to a StringBuilder to index into and replace some of the characters only to copy it back to the ref'd param. Is it possible to index directly into the text param as an L-Value?

Or how else can I improve this?

Upvotes: 1

Views: 417

Answers (4)

JamesSugrue
JamesSugrue

Reputation: 15011

Armed with Reflector and the decompiled IL - On a pure LOC basis then the StringBuilder approach is definitely the most efficient. Eg tracing the IL calls that StringBuilder makes internally vs the IL calls for String::Remove and String::Insert etc.

I couldn't be bothered testing the memory overhead of each approach, but would imagine it would be in line with reflector results - the StringBuilder approach would be the best.

I think the fact the StringBuilder has a set memory size using the constructor

StringBuilder sb = new StringBuilder(text);

would help overall too.

Like others have mentioned, it would come down to readability vs efficiency...

Upvotes: 3

John Sheehan
John Sheehan

Reputation: 78104

I don't know if this is more efficient, but it works. Either way you'll have to recreate the string after each change since they're immutable.

    string test = "hello world";
    Console.WriteLine(test);

    test = test.Remove(5, 1);
    test = test.Insert(5, "z");

    Console.WriteLine(test);

Or if you want it more concise:

string test = "hello world".Remove(5, 1).Insert(5, "z");

Upvotes: -1

Factor Mystic
Factor Mystic

Reputation: 26760

text = text.Substring(0, 4) + "a" + text.Substring(5);

Not dramatically different than your StringBuilder solution, but slightly more concise than the Remove(), Insert() answer.

Upvotes: -1

Andru Luvisi
Andru Luvisi

Reputation: 25308

C# strings are "immutable," which means that they can't be modified. If you have a string, and you want a similar but different string, you must create a new string. Using a StringBuilder as you do above is probably as easy a method as any.

Upvotes: 3

Related Questions