不从字符串中删除子字符串

aaa*_*aaa 0 c# substring

我试图在这个简单的代码中从字符串中删除子字符串.但是c#没有删除它:

stringCmd = "Haha WoWI am in love!"
stringCmd.Remove(stringCmd.IndexOf("WoW"), 5);
Run Code Online (Sandbox Code Playgroud)

删除后应该是"哈哈恋爱!"

Ale*_*.P. 7

字符串在.NET中是不可变的

stringCmd = "Haha WoWI am in love!"
stringCmd = stringCmd.Remove(stringCmd.IndexOf("WoW"), 5);
Run Code Online (Sandbox Code Playgroud)


Mar*_*zek 6

string.Remove 方法返回新字符串而不修改作为参数传递的字符串,因此您必须将其分配回您的变量:

stringCmd = stringCmd.Remove(stringCmd.IndexOf("WoW"), 5);
Run Code Online (Sandbox Code Playgroud)

您还应该知道string.NET中的s是不可变的.您可以在MSDN上阅读更多相关信息:string(C#参考).