我正在寻找一种方案的标准/最佳实践,我需要根据值列表检查相同的属性值,如果任何值与该属性匹配,则返回true。需要更好的方法来比较多个值与属性并返回true;
目前的代码类似这样(我没有写出来,我期待重构它)...
if (object.property == "string1"
|| object.property == "string2"
|| object.property == "string3"
|| object.property == "string4"
|| object.property == "string5"
|| object.property == "string6"
|| object.property == "string7"
|| object.property == "string8"
|| object.property == "string9"
|| object.property == "string10"
|| object.property == "string11"
|| object.property == "string12"
|| object.property == "string13"
|| object.property == "string14"
|| object.property == "string15")
如果你已经常量字符串 - 比使用'switch-case'结构。 – 2012-01-27 23:34:41
所有有效的答案,比我提供的更好。干得好的人。我打算给安德鲁我的投票,因为它看起来是最可读的,应该使用字符串数组分配最少量的内存。再次非常感谢伟大的答案。 – TXRAckAck 2012-01-27 23:45:56