【问题标题】:Contains &/ Replace HELP NEEDED (C#)包含和/替换 HELP NEEDED (C#)
【发布时间】:2017-01-26 09:17:44
【问题描述】:
        if (b.Text == "+/-"){
            if (resultField.Text.Contains("-"))
            {
                resultField.Text.Replace("-", "");
            }else{
                resultField.Text = "-" + resultField.Text;
            };
            return;
        };

此代码不起作用,它旨在切换计算器上的负值。

【问题讨论】:

  • 请为问题添加更多上下文。它没有以可重现的方式描述您的问题

标签: c# replace contains


【解决方案1】:

我认为你想要上面的第一个 if 案例。

resultField.Text = resultField.Text.Replace("-", "");

【讨论】:

    【解决方案2】:

    试试:

     resultField.Text = ResultField.Text.IndexOf("-")>-1?ResultField.Text.Replace("-",""):"-"+ResultField.Text;
    

    这是一个语法快捷方式,表示如果文本包含“-”然后替换 else 将“-”添加到文本值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-15
      • 2019-05-25
      • 2013-09-11
      • 2016-09-03
      • 1970-01-01
      • 2019-07-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多