简体   繁体   中英

How to check if a StringComparison is case sensitive?

Given a StringComparison instance, how should I check if it is case sensitive?

Should I compare it to all currently known case sensitive values in the enum?

StringComparison sc = ...;
bool isCaseSensitive = false
    || sc == StringComparison.CurrentCulture
    || sc == StringComparison.InvariantCulture
    || sc == StringComparison.Ordinal;

Yes, for the enum form ( StringComparison ) that's fine. MS is incredibly unlikely to add another StringComparison value now, given that adding a value to an enum is effectively a breaking change.

For a StringComparer , it's rather harder :(

Well, yes. StringComparison is an enum with 6 values, and there is no method to test them for their case sensitiveness.

You have to do it your own, and your method seems reasonable.

If you want a (nice | nasty) one-liner:

bool isCaseSensitive = (int)StringComparison.Ordinal % 2 == 1;

您实际上如何使用提供的枚举来比较两个字符串?

bool isCaseSensitive = !("A".Equals("a", sc));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM