Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.Insert not working [duplicate]

Tags:

c#

xna

I have (hopefully) a simple problem.

String.Insert(0, "test");

is not working, yet:

String.Text += "test";

does..? I need Insert so I can insert something in the middle of a string.

like image 355
Dean Reynolds Avatar asked Sep 01 '26 04:09

Dean Reynolds


2 Answers

I'm such an idiot, instant Google search fixed this.

String.Insert does not modify the string it is called on, but instead returns a new string with the inserted text in.

So I needed:

String = String.Insert(0, "test");
like image 118
Dean Reynolds Avatar answered Sep 03 '26 20:09

Dean Reynolds


it should work

see the below example

string names = "Romeo Juliet";
string shakespeare = names.Insert(6, "and ");

Console.WriteLine(shakespeare);

the result will be Romeo and Juliet

see the fiddle

like image 36
Sachu Avatar answered Sep 03 '26 20:09

Sachu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!