Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to handle null values in regx.ismatch

C#
System.Text.RegularExpressions.Regex.IsMatch(business.Cuisine,"chicken", System.Text.RegularExpressions.RegexOptions.IgnoreCase)


if the input null how to handle this regx.ismatch

please help me.
Thank u.

What I have tried:

C#
System.Text.RegularExpressions.Regex.IsMatch(business.Cuisine,"chicken", System.Text.RegularExpressions.RegexOptions.IgnoreCase)


if the input null how to handle this regx.ismatch
Posted
Updated 24-Dec-18 2:07am
Comments
Patrice T 24-Dec-18 7:54am    
What do you need to do with that null input ?
Krishna Veni 24-Dec-18 7:59am    
For suppose business.cuisine==null.cuisine access from db.at the time get System.ArgumentNullException occured

1 solution

Check for null before you try and do the regex, just like you do for all other places where a null is possible. In some case, provided you are using a new enough version of C#, you can use the safe navigation operator "?.": At last, C# is getting “?.”, sometimes called the Safe Navigation Operator – Jerry Nixon[^] But you can't here, because IsMatch doesn't like null arguments. Instead, use the null-coalescing operator '??'
C#
Regex.IsMatch(business.Cuisine ?? "","chicken", RegexOptions.IgnoreCase)
Or even:
C#
Regex.IsMatch(business?.Cuisine ?? "","chicken", RegexOptions.IgnoreCase)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900