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)码字不易,转载请标注来源,感谢!