在字符串中插入字符串

时间:2014-07-02 01:42:50

标签: c# string insert

好的,这真的应该很简单,我不知道它为什么不起作用。当我尝试对字符串使用Insert方法时,似乎什么都不做。

                MonthAge = mAge.ToString();

                newLine = rl.Substring(0, rl.Length);
                newLine.Insert(32, MonthAge + ",");  //doesnt do anything
                newLine.Insert(0, "DOSOMETHING");   //doesnt do anything

我确信这是一个简单的解决方案,但在浏览了几个线程之后我找不到它。

2 个答案:

答案 0 :(得分:1)

字符串在C#中不可变。 Insert返回一个新字符串,而不是修改现有字符串。

这应该有效:

newLine = newLine.Insert(32, MonthAge + ",");

答案 1 :(得分:1)

您没有将返回值分配给任何内容 - .NET字符串是不可变的,Insert返回string的新实例。

相关问题