使用C#,我如何确保下面代码中的'val'对象是小写的?
case MultiValueUpdateMode.AddIfNotExist:
if (value != null)
{
// Add the values only if they do not exist
foreach (var val in newValues)
{
if (!userDe.Properties["proxyAddresses"].Contains(val))
userDe.Properties["proxyAddresses"].Add(val);
}
}
break;
答案 0 :(得分:2)
您可以使用ToString()
将 任何 对象转换为该字符串,然后使用ToLower()
将其设置为低级字符串:
string lowerCasedString = val?.ToString()?.ToLower(); //put null coaelescing just in case it is null
然后像这样使用它:
if (!userDe.Properties["proxyAddresses"].Contains(lowerCasedString))
附加说明:我通常也会使用Trim()
删除前导空格和后续空格:
string cleanLowerCasedString = val?.ToString()?.ToLower()?.Trim(); //put null coaelescing just in case it is null
答案 1 :(得分:0)
case MultiValueUpdateMode.AddIfNotExist:
if (value != null)
{
// Add the values only if they do not exist
foreach (var val in newValues)
{
var lowerCaseVal = val.ToLower();
if (!userDe.Properties["proxyAddresses"].Contains(lowerCaseVal))
userDe.Properties["proxyAddresses"].Add(lowerCaseVal);
}
}
break;