如果blank替换为null,则动态检查条件

时间:2016-07-04 09:27:45

标签: c#

C#

    Class x
    {
    string Insurer{get;set;}
    }
Now,

x.Insurer=txtInsurer.Text; 

现在文件strText包含@Insurer替换为x.Insurer

strText = strText.Replace("@Insurer","\""+x.Insuerer+"\"");

strText的输出是

保险人:"",

我的要求是如果txtInsurer.Text为空,则strText文件包含@Insurer replaces with null i.e Insurer:null,

instead of Insurer:"",

1 个答案:

答案 0 :(得分:2)

使用三元运算符:

strText = strText.Replace("@Insurer", x.Insuerer == null 
  ? "NULL" 
  : ("\"" + x.Insuerer + "\""));