c#中string的remove方法执行没有用

来源:百度知道 编辑:UC知道 时间:2024/06/17 17:02:15
using System;
using System.Collections.Generic;
using System.Text;

namespace StringRemove
{
class Program
{
static void Main(string[] args)
{
string temp = "hehe;";
Console.Write(temp + "\n");
temp.Remove(temp.Length - 1);
Console.Write(temp + "\n");

System.Threading.Thread.Sleep(10000);
}
}
}

我是想要移除这个分号,但是始终没有移除 这个remove方法没有用?还是该用其他的方法?

Remove完了是一个新字符串。。要再赋值回去。。

temp=temp.Remove(temp.Length - 1);

还有楼上那个也没加。。
temp=temp.SubString(0,temp.Length - 1);

方法是写对了,只是修改后没赋值过去
string temp = "hehe;";
Console.Write(temp + "\n");
temp = temp.Remove(temp.Length - 1); //赋值
Console.Write(temp + "\n");
像你声明的这些变量(string temp=“”)他们只有获得(string temp=“1”)和输出(string i=temp)等功能,他们没有自动改变值(temp.Remove(temp.Length - 1))的功能,把变量修改完以后,必须得再次赋给他修改后的值(temp = temp.Remove(temp.Length - 1))
其他的变量都是一样的

string temp = "hehe;";
Console.Write(temp + "\n");
temp.SubString(0,temp.Length - 1);
Console.Write(temp + "\n");

System.Threading.Thread.Sleep(10000);

二楼正解