如何在regx.ismatch中处理空值 [英] How to handle null values in regx.ismatch

查看:98
本文介绍了如何在regx.ismatch中处理空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何处理regx.ismatch中的空值



how to handle null values in regx.ismatch

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





如果输入null如何处理这个regx.ismatch



请帮帮我。

谢谢你。



我尝试过:





if the input null how to handle this regx.ismatch

please help me.
Thank u.

What I have tried:

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





如果输入null如何处理这个regx.ismatch



if the input null how to handle this regx.ismatch

推荐答案

在尝试执行正则表达式之前检查 null ,就像对所有其他地方一样null是可能的。在某些情况下,如果您使用的是足够新版本的C#,则可以使用安全导航操作符?。:最后,C#正在变成?,有时被称为安全导航操作员 - 杰里·尼克松 [ ^ ]但是你不能在这里,因为IsMatch不喜欢null参数。相反,使用null-coalescing运算符'??'

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 '??'
Regex.IsMatch(business.Cuisine ?? "","chicken", RegexOptions.IgnoreCase)

甚至:

Or even:

Regex.IsMatch(business?.Cuisine ?? "","chicken", RegexOptions.IgnoreCase)


这篇关于如何在regx.ismatch中处理空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆