包含不正常 [英] Contains not working properly

查看:83
本文介绍了包含不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从数据库中获取了一个列值,该值包含另一个字符串



代码

i had get the one column values from database that value contains another string or not

code

String.Contains(business.Cuisine,"chicken",StringComparison.OrdinalIgnoreCase)





包含未显示的方法intelisense报告,这就是为什么我有实现contains方法

此方法无法正常工作。假设源为null。如何处理和忽略这种情况。





contains method not shown intelisense report thats why i have implement contains method
This method not working propely.For suppose if the source is null. how to handle and ignore the case.

public static bool Contains(this string source, string toCheck, StringComparison comp)
      {
          if (source == null)
              return false;
          return source.IndexOf(toCheck, comp) >= 0;
      }





请帮帮我。

谢谢你。



我尝试过:





Please help me.
Thank u.

What I have tried:

public static bool Contains(this string source, string toCheck, StringComparison comp)
      {
          if (source == null)
              return false;
          return source.IndexOf(toCheck, comp) >= 0;
      }

推荐答案

引用:

包含未显示的方法intelisense

contains method not shown intelisense



那是因为包含不是静态方法:它是一个实例方法,所以你在实际字符串上调用它:


That's because Contains is not a static method: it's an instance method so you call it on the actual string:

if (business.Cuisine.Contains("chicken"))
   {
   ...



并且没有包含方法,允许您指定不区分大小写。

但是IndexOf方法deos,正如您所发现的那样在互联网上。



但正如我在你用Regexes尝试时向你解释的那样:

如何处理regx.ismatch中的空值 [ ^ ]

您需要自己检查和处理空值!系统不会为你做那件事 - 这是设计上的,而且是完全正确的......



请问,在104个问题之后,你应该对此非常称职现在的东西,不是一遍又一遍地犯同样的错误......这让你看起来就像你在这里学到的东西一样,这只会让你成为帮助吸血鬼。


And there is no Contains method that allows you to specify case insensitivity.
But the IndexOf method deos, as you have found on the internet.

But as I explained to you when you were trying it with Regexes:
How to handle null values in regx.ismatch[^]
You need to check and handle nulls yourself! The system will not do that for you - that is by design, and is absolutely correct...

Please, after 104 questions, you should be pretty competent in this stuff now, not making exactly the same mistake over and over again ... which make you look like you are learning absolutely nothing here, and that would just make you a Help Vampire.


< a href =https://docs.microsoft.com/en-us/dotnet/api/system.string.indexof?view=netframework-4.7.2#System_String_IndexOf_System_String_System_StringComparison_> String.IndexOf Method(System)| Microsoft Docs [ ^ ]

String.IndexOf Method (System) | Microsoft Docs[^]
public static bool Contains(this string source, string toCheck, StringComparison comp)
      {
          if (source == null)
              return false;

          return source.IndexOf(toCheck, 0, source.Length, comp) >= 0;
      }


这篇关于包含不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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