Reputation: 103487
string.insert in c# doesn't overwrite the character that is in the startindex does it?
Upvotes: 2
Views: 3846
Reputation: 46173
No. Strings are immutable. string.Insert
returns a new string with the inserted value. It does not change the old string.
string newString = oldString.Insert(3, "foo");
oldString
does not change. But "foo" is now in newString
.
Upvotes: 2
Reputation: 7282
No, definitely not.
But the best practice is to use StringBuilder instead of immutable strings if you're doing changes to the string in-flight.
Here's a nice overview.
Upvotes: 0
Reputation: 75982
Insert does addition only. Replace does change.
Edit: As others pointed out, strings are actual immutable, so both methods will return copy of your initial string. However, the semantic of the operations is as above.
Upvotes: 2
Reputation: 9146
No. It just tells it where to insert the character, so if you had the following
string x = "ello".Insert(0, "h");
the string would actually read "hello".
Upvotes: 1
Reputation: 36438
Strings in c# are immutable. They cannot be changed except by reflection or unsafe code (and you should never do this).
All methods on string which 'modify' it instead return a new string with the appropriate modifications.
Since insert is placing one string within another the result of an insert of string s1
into string s2
will be a string of length s1.Lnegth + s2.Length
, no characters are lost.
Upvotes: 1
Reputation: 16680
For example, the return value of "abc".Insert(2, "XYZ") is "abXYZc".
So, no.
http://msdn.microsoft.com/en-us/library/system.string.insert.aspx
Upvotes: 7