日期:2014-05-17  浏览次数:21437 次

C#下string.Remove和Replace()无效
小菜今天自学C#然后对一个string类型的对象操作,一开始想删除开头的字母,然后将字符串中的某个字母替换掉,可是试过没有用,本菜初学,求大神指教,感激不尽。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tesing
{
    class Program
    {
        static void Main(string[] args)
        {
            string test = "He+llo world!)";
            string u;
            u = test.Substring(0, 1);
            test.Remove(1);
            Console.WriteLine("剪切下来的字符串为{0},Test字符串的大小为{1}",u,test.Length);
            int Cout;
            char[] Signe = new char[] { '(', ')', '+', '-', '*', '/' };
            test.Replace("!", "#");
            Console.WriteLine("执行完Replace函数后的test:{0},它的长度{1}", test, test.Length);
            Cout = test.IndexOfAny(Signe,0);
            Console.WriteLine(Cout);
            Console.ReadLine();
        }
    }
}

程序的运行结果是
c# string

------解决方案--------------------
test = test.Remove(1);
------解决方案--------------------
test.Remove(1);
=>
test = test.Remove(1);

c#的系统类里,有些类的操作不改变自身,需要重新赋值,比如string,int
另一些是直接改变自身的,比如StringBuilder,List,不需要写成 l=l.Remove()
这些需要记住

当然你写int i = 0; i+1; 会直接编译错误。但你的test.Remove(1)连个警告都没有让我对编译器非常不爽