Reputation: 539
I understood that when using Regex I can enter the value |
to search for many values. For example:
Regex sabrina = new Regex("Rihanna|rihanna|Sabrina|sabrina");
I have a string which I want to compare to other values, so I use if
like this:
if (rihanna == "Rihanna" || rihanna == "sabrina")
My question is if it possible to make the if
shorter? I know this code is not working but what I'm looking for something like this:
if (rihanna == "Rihanna|sabrina")
Upvotes: 1
Views: 170
Reputation: 100545
Dictionary or HashSet (potentially with case insensitive check on names) is another approach which will give you better performance if you have more than several items and repeat checks often.
var listToCheck = new Dictionary<string, string>(
StringComparer.CurrentCultureIgnoreCase) { { "rihanna", null}};
if (listToCheck.ContainsKey("Rihanna")) ....
Upvotes: 0
Reputation: 23218
As an alternative to the above answers, perhaps you could consider using an extension method:
public static bool EqualsAny(this string input, params string[] values)
{
return values.Any(v => String.Equals(input, v, StringComparison.OrdinalIgnoreCase));
}
With usage like:
if (rihanna.EqualsAny("Rihanna", "Sabrina"))
This should give pretty readable/compact/short if
comparisons for you, especially in comparison to some of the other alternatives here.
Tweak the EqualsAny
check if you want to have the OrdinalIgnoreCase
or not or feel free to add any overloads you wish. Could even implement the whole piping/splitting where you'd pass in "Rihanna|Sabrina".
EDIT: Perhaps more critically, it centralizes how you compare rather than duplicating the code everywhere.
Upvotes: 1
Reputation: 35407
Ignore casing, which looks like something you're going for, and use String.Compare
:
if(String.Compare(rihanna, "sabrina", StringComparison.OrdinalIgnoreCase) == 0 ||
String.Compare(rihanna, "rihana", StringComparison.OrdinalIgnoreCase) == 0 ) {
// do stuff
}
Upvotes: 0
Reputation: 8780
You can use your own Regex and use it as a single line..
if (Regex.Match(rihanna, "Rihanna|rihanna|Sabrina|sabrina").Success)
Or make it case-insensative like this..
if (Regex.Match(rihanna, "rihanna|sabrina", RegexOptions.IgnoreCase).Success){
Upvotes: 0
Reputation: 112855
You could use a List<string>
along with the Enumerable.Contains
method.
Example:
var values = new List<string> { "Rihanna", "rihanna", "Sabrina", "sabrina" };
if (values.Contains(rihanna))
{
// etc.
}
The if
statement is now a lot more concise, and you can add more items to your list quickly and easily. If you're performing this operation frequently, you should move the declaration of the list to a member variable in the containing class so that you don't incur the overhead of list initialization every time this method is called.
An even better solution would be to use Enumerable.Any
and String.Equals
to perform a case-insensitive comparison:
if (values.Any(x => String.Equals(rihanna, x, StringComparison.OrdinalIgnoreCase))
{
// your code here
}
Although the if
statement is now longer, the solution is much more robust.
Upvotes: 2
Reputation: 29745
If you're looking for easier maintenance of a potentially long list of string candidates, why not use a switch statement? Technically it will be "longer" character-wise, but if you have to modify things down the road, or provide some subtle secondary processing, it could make things a lot easier.
Upvotes: 1
Reputation: 125650
Single line solution:
if("Rihanna|rihanna|Sabrina|sabrina".Split(new[] {'|'}).Contains(rihanna))
or without String.Split
method usage:
if ((new[] { "Rihanna", "rihanna", "Sabrina", "sabrina" }).Contains(rihanna))
If that kind of check is performed more than once you should save string[]
array of possible values into a variable:
var names = new[] { "Rihanna", "rihanna", "Sabrina", "sabrina" };
if(names.Contains(rihanna))
Upvotes: 5