【问题标题】:Insert string inside a string [duplicate]在字符串中插入字符串[重复]
【发布时间】:2014-07-02 01:42:49
【问题描述】:

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

                MonthAge = mAge.ToString();

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

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

【问题讨论】:

标签: c# string insert


【解决方案1】:

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

这应该可行:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 谢谢,就像我说的,很容易解决。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    相关资源
    最近更新 更多