Insert

  • Thread starter Thread starter csharpula csharp
  • Start date Start date
C

csharpula csharp

Hello ,
I am trying to do something like that:

string name="";
name.Insert(name.Length,args);

and there is a string value in args but the value of the name after
insertion is still "" and the args value wasn't inserted. why?
thank u!
 
csharpula said:
Hello ,
I am trying to do something like that:

string name="";
name.Insert(name.Length,args);

and there is a string value in args but the value of the name after
insertion is still "" and the args value wasn't inserted. why?
thank u!


Because the string is immutable in C#.

So, what you need to do is :

string insertedString=name.Insert(name.Length,args); and the
insertedString is what you want.

HTH.

J.W.
 
That is not helping,I still get an empty string. Maybe it's because my
start index in Insert is 0?
 
csharpula csharp said:
That is not helping,I still get an empty string. Maybe it's because my
start index in Insert is 0?

It works just fine for me.

Here is a sample using the idea behind Jianwei's reply:

string arg = "test";
string name = "";
name = name.Insert(name.Length, arg);
Console.WriteLine(name);

The Insert method of the string class is a method that returns the resulting
string. It does not modify the string you are inserting into, but instead
returns the string with the inserted value in it.

Just like all methods of the string class, the original string doesn't
change.

HTH,
Mythran
 
Back
Top