简体   繁体   English

如何使用具有SQL LIKE条件功能的C#contains方法?

[英]How can I use C# contains method with the functionality of sql LIKE condition?

I want to be able to use the contains function the same way the like operator works in sql. 我希望能够像在SQL中使用类似运算符一样使用contains函数。 So when I call .contains(%This%%%%%%is%%my%string%%) from a list or whatever such as "This is my string " then the function will return true. 因此,当我从列表或诸如“ This is my string”之类的列表中调用.contains(%This %%%%%% is %% my%string %%)时,该函数将返回true。 I've done a lot of searching and it seems like a lot of people would like this function. 我已经做了很多搜索,似乎很多人都希望使用此功能。 So how can this be done or how can a custom like function with the same functionality be made? 那么如何做到这一点,或者如何制作具有相同功能的类似自定义功能?

EDIT Thank you, for the quick response. 编辑谢谢您的快速回复。 I was able to use a regular expressions inside of my own custom Like function. 我可以在自己的自定义Like函数中使用正则表达式。 Below is the code for anyone else who wants to use something similar to SQL Like. 以下是其他人想要使用类似于SQL Like的代码。 In this code the user would input the databaze value and then spaces in that value are replaced with .* to ignore anything in-between the values.Just like using the % to replace spaces and values in SQL. 在此代码中,用户将输入数据库值,然后用。*替换该值中的空格以忽略值之间的任何内容,就像使用%替换SQL中的空格和值一样。 I can then use .Like on my string value called testValue that I am searching through to return true or false depending on if the words or whatever are in my string. 然后,我可以在搜索的字符串值testValue上使用.Like,以返回true或false,具体取决于单词或字符串中的内容。 I also added ToUpper to ignore the case. 我还添加了ToUpper来忽略这种情况。 //C# Like function // C#Like函数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace sqllinqstuff
{


class Program
{
    static void Main(string[] args)
    {
        string testValue = "Big RED           Car";

        string databaze = "big red CAR";

        string databaze3 = databaze.Replace("     ", ".*");
        databaze3 = databaze3.Replace(" ", ".*");

        Console.WriteLine(databaze3);
        Console.WriteLine(testValue.Like(databaze3)); 

Console.Read();

    }
}



public static class CaseyStringExtension
{
    public static bool Like(this string str,string value)
    {
        if (!string.IsNullOrEmpty(str))
        {
            Regex r = new Regex(value.ToUpper());
                if (r.IsMatch(str.ToUpper()))
                    return true;

        }

        return false;
    }
}

}

The result of this test will be true. 该测试的结果将是正确的。

The way this would be done is with Regex . 使用Regex可以做到这一点。 The syntax is not the same as a SQL LIKE query so there will be a bit of a learning curve. 语法与SQL LIKE查询不同,因此会有一些学习上的困难。 This is a good tutorial site I often reference, it can get you started with the basics. 这是我经常引用的一个很好的教程网站 ,它可以帮助您入门。

Translating your original string you asked about, the regex search pattern would be 翻译您询问的原始字符串,正则表达式搜索模式为

.*This.*is.*my.*string.*

Once you get good at it you can do searches like this one I helped create for a password complexity checker 一旦您精通它,就可以进行这样的搜索,这是我帮助创建的密码复杂性检查器

(?=^.{8,}$)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W_])(?=^.*[^\s].*$).*$

The above search looks for a string that has at least 8 characters with at least one lower case letter, one upper case letter, one special character, and no white-space characters. 上面的搜索将查找一个字符串,该字符串至少包含8个字符,其中至少一个小写字母,一个大写字母,一个特殊字符以及没有空格字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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