String.insert in C # doesn't overwrite?

string.insert in C # doesn't overwrite the character that is in startindex?

+2


source to share


6 answers


For example, the return value is "abc" .Insert (2, "XYZ") is "abXYZc".

So no.



http://msdn.microsoft.com/en-us/library/system.string.insert.aspx

+7


source


Insert only adds. Replace changes.



Edit . As others have pointed out, strings are valid immutable, so both methods will return a copy of the original string. However, the semantics of the operations are the same as above.

+2


source


Not. Strings are immutable. string.Insert returns a new string with the inserted value. It doesn't change the old line.

string newString = oldString.Insert(3, "foo);

      

oldString does not change. But "foo" is now in newString.

+2


source


Strings in C # are immutable. They cannot be changed except by reflection or unsafe code (and you should never do that).

All methods on the line that "change" instead return a new line with the appropriate changes.

Since insertion puts one string on top of another, the result of inserting a string s1

into a string s2

will contain the string length s1.Lnegth + s2.Length

, no characters will be lost.

+1


source


Not. It just tells you where to insert the symbol, so if you had the following

string x = "ello".Insert(0, "h");

      

the line will actually read "hello".

+1


source


No, definitely not.

But it is best to use StringBuilder instead of immutable strings if you are making changes to the string in flight.

Here's a good overview.

0


source







All Articles