如题.比如 string temp = "<p> &nbsp;hello world &nbsp;&nbsp;",我想要的结果是:
temp = "<p> hello world &nbsp;&nbsp;"

解决方案 »

  1.   


    void Main()
    {
    string temp = "<p> &nbsp;hello world &nbsp;&nbsp;";
    int i=1;
    temp=Regex.Replace(temp,"&nbsp;",delegate(Match m){
      if(i==1) {i++;return "";}
      else{return "&nbsp;";}
    });
    Console.WriteLine(temp);//<p> hello world &nbsp;&nbsp;

    }论坛签名======================================================================silvertoss:你好!
    截至 2011-03-17 10:22:30 前:
    你已发帖 13 个, 未结贴 4 个;
    结贴率为: 69.23%

    当您的问题得到解答后请及时结贴.

    http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
    http://topic.csdn.net/u/20100428/09/BC9E0908-F250-42A6-8765-B50A82FE186A.html
    http://topic.csdn.net/u/20100626/09/f35a4763-4b59-49c3-8061-d48fdbc29561.html如何给分和结贴?
    http://community.csdn.net/Help/HelpCenter.htm#结帖如何给自己的回帖中也加上签名?
    http://blog.csdn.net/q107770540/archive/2011/03/15/6250007.aspx
      

  2.   


    //RT,删除字符串的第一个空格
    static string DeleteFirstSpace(string yourStr) 
            {
                string strResult = "";
                for (int i = 0; i < yourStr.Length; i++) 
                {
                    if (yourStr[i] == ' ') 
                    {
                        if (i == 0)
                        {
                            strResult = yourStr.Substring(i + 1);
                        }
                        else if(i!=yourStr.Length-1)
                        {
                            strResult = yourStr.Substring(0, i) + yourStr.Substring(i + 1);
                        }
                        else if (i == yourStr.Length - 1) 
                        {
                            strResult = yourStr.Substring(0, yourStr.Length - 2);
                        }
                        break;
                    }
                }
                return strResult;
            }//测试:
    Console.WriteLine(DeleteFirstSpace("hello world! sss"));
    //输出结果为:
    //helloworld! sss
      

  3.   

    正则上面有人说了
    我给你个更简单的呵呵:
    string temp = "<p> &nbsp;hello world &nbsp;&nbsp;";
    temp=temp.Replace("<p>&nbsp;","<p>");
      

  4.   

    直接用String对象的Trim()方法不可以吗?
      

  5.   

    str.Replace('&nbsp',''); 就可以了啊 有必要那么复杂么
      

  6.   

    哈哈,来个正则版 的:string temp = "<p> &nbsp;hello world &nbsp;&nbsp;";
    temp = Regex.replace(temp,@"(?<!&nbsp;.*)&nbsp;")
      

  7.   

    string temp = "<p> &nbsp;hello world &nbsp;&nbsp;"; 
    if (temp .charAt(0) == " ")
      {
        //如果字串左边第一个字符为空格
        str = str.slice(1);//将空格从字串中去掉
        //这一句也可改成 str = str.substring(1, str.length);
        str = lTrim(str);    //递归调用
      }