从asp.net中的字符串中删除最后一个字符无效

时间:2016-11-09 05:21:55

标签: c# asp.net replace

我想从调试后的字符串中删除,

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);
        }

但保存时却给我错误

  

号码无效

4 个答案:

答案 0 :(得分:0)

使用此代码..

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

如果您只想删除最后一个

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

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

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

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