有一个字符串“str=1 3 45. 7 8 9 0 5”,要去除其中所有的空格,使字符串变为“str=1345.78905”,这个方法应该怎样写,都有那些不同的方法???

解决方案 »

  1.   

    str.replace(" ","")
    这个方法就可以解决
      

  2.   

    str.Replace(" ", "")
    Regex.Replace(str,@"\s","")
      

  3.   

    替换的方式
    string str = "1 3 45. 7 8 9 0 5";
            Response.Write(str.Replace(" ",""));
      

  4.   

    自己看,方法很多的
    http://www.cnblogs.com/kym/archive/2009/12/07/1618293.html
      

  5.   

      myDoc.value=myDoc.value.replace(/\s/gi,"");//如果对象内容有任何空白字符,包括空格、制表符、换页符等等全文查找、忽略大小写,就把它清空
      

  6.   


    //最常用的就是这个了
    str.Replace(" ", "")
      

  7.   

    由于空格的ASCII码值是32,因此,在去掉字符串中所有的空格时,只需循环访问字符串中的所有字符,并判断它们的ASCII码值是不是32即可。去掉字符串中所有空格的关键代码如下:下面提供一个非Replace方法实现的方法,如下using System.Collections;    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string s = "str=1 3 45. 7 8 9 0 5";
                Response.Write(tripBlank(s));
            }
        }    public string tripBlank(string s)
        {
            string newstr = string.Empty;
            CharEnumerator ce = s.GetEnumerator();
            while (ce.MoveNext())
            {
                byte[] array = new byte[1];
                array = System.Text.Encoding.ASCII.GetBytes(ce.Current.ToString());
                int asciicode = (short)(array[0]);
                if (asciicode != 32)
                {
                    newstr+= ce.Current.ToString();
                }
            }
            return newstr;
        }
      

  8.   

    replace
    超简单的说
    如果要写入数据库,还可以用RARIM函数,
      

  9.   


    str.replace(/\s/gi,"")应该OK了
      

  10.   

    string.Join("", str.Split(' '))这种也可以
      

  11.   


    str.Replace(" ", "");
      

  12.   

    若想连汉字的空格,一块去掉,可以:str="1 23  . 4 5 6";
    str=str.Replace(" ", "").Replace(" ", "")
      

  13.   

    string str="1 3 45. 7 8 9 0 5";
    string newStr = str.replace(" ", string.Empty);
      

  14.   

    正则 会必str.Replace(" ", "");
    快一些