C# Contains判断空字符

C#字符串Contains注意事项

1、判断字符是否包含字符(Contains)

int subIndex = Feature.Fields.FindField("Subsid");
if (subIndex != -1 && subs.Contains(Feature.get_Value(subIndex).ToString()))
{
  isCount = true;
}

1)Contains不能判断空字符,默认为真

subs: ”阀门“   字符串

Feature.get_Value(subIndex).ToString())   ""

subs.Contains(Feature.get_Value(subIndex).ToString())   true

运行结果:

"阀门".Contains("")
true

2)正确用法

  • string.IsNullOrEmpty()
//字段不存在判断
int subIndex = Feature.Fields.FindField("Subsid");
string subStr=Feature.get_Value(subIndex).ToString();
//为空判断
if (subIndex != -1 && !string.IsNullOrEmpty(subStr) && subs.Contains(Feature.get_Value(subIndex).ToString()))
{
  isCount = true;
}
  • ( subs == ""
//字段不存在判断
int subIndex = Feature.Fields.FindField("Subsid");
string subStr=Feature.get_Value(subIndex).ToString();
//为空判断
if (subIndex != -1  && subs == (Feature.get_Value(subIndex).ToString()))
{
  isCount = true;
}

2、数组使用 Contains 可以判断空字符串

>> unitNames.Contains("")
false
>> unitNames
Count = 3
    [0]: "河流"
    [1]: "道路"
    [2]: "房屋"

声明:

1)经过项目实践验证,文章存在错误或疑问请留言,感谢您的阅读!

2)码字不易,转载请标注来源,感谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值