String.Insert not working [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-17 21:38:28

问题


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.


回答1:


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");



回答2:


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



来源:https://stackoverflow.com/questions/31216892/string-insert-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!