【问题标题】:Remove last character from string in asp.net not working从 asp.net 中的字符串中删除最后一个字符不起作用
【发布时间】:2017-03-22 21:13:48
【问题描述】:

我想从我得到的字符串中删除,,就像调试时一样

27/1, 106/2,

注意,后面还有空格。

所以。我试过下面的代码

if (txt712.Text != "")
        {
           string strText = txt712.Text;

           strText = strText.Remove(strText.Length - 2, 1);
           xw.WriteElementString("SURVEY_AREA_7_12", strText);
        }

但是在保存时却给了我错误

号码无效

【问题讨论】:

  • 您要删除所有“,”还是只删除最后一个。

标签: c# asp.net replace


【解决方案1】:

如果你只想删除最后一个

 str = str.Substring(0, str.Length - 1);

如果要删除所有的“,”和最后一个空格

    str = str.Trim().Replace(",", "");

在您的代码中,我不确定 xw.WriteElementString 的作用。 所以我认为还有其他一些逻辑错误,xw.WriteElementString 不接受字符串值。

【讨论】:

    【解决方案2】:

    strText = strText.Trim().replace(',','');

    【讨论】:

      【解决方案3】:

      使用Trim() 将删除字符串中的空格,Regex.Replace 用于替换,

                  if (txt712.Text != "")
                  {
                      string strText = txt712.Text;
                      strText = Regex.Replace(strText.Trim(),",", "");   
                      xw.WriteElementString(strText);
                  }
      

      【讨论】:

        【解决方案4】:

        使用此代码..

        var str = "127/10,";
        var TrimedStr= str.substring(0, str.length-1);
        

        【讨论】:

          猜你喜欢
          • 2017-08-19
          • 2017-01-11
          • 2011-01-19
          • 1970-01-01
          • 2021-12-11
          • 2010-11-08
          相关资源
          最近更新 更多