2

I have two strings for comparison

String Str1 = "A C";
String Str2 = "A B C";
Str2.Contains(Str1); //It will return False ,Contains append % at Start and End of string 

//Replace space with %
Str1 = "%A%C%"; 
Str2 = "%A%B%C%";
Str2.Contains(Str1); //Want it to return True ,

We do have Contains,StartsWith,EndsWith methods for comparison, But what my requirement is , if we compare str2 and str3 , it should return True , as it lies in Str2.

Can we achive such behaviour in C# ?I have done this in SQL but not getting some useful at C#.Any regex etc ?

Ehsan Ullah Nazir
  • 1,827
  • 1
  • 14
  • 20
  • Yes, you can use regular expressions in .NET - have you tried that yet? If so, please show what you've tried... – Jon Skeet Feb 09 '17 at 07:26
  • what would be the expected result if `Str2 = "A B C D"` and `Str1 = "A C";` – sujith karivelil Feb 09 '17 at 07:26
  • See [Matching strings with wildcard](http://stackoverflow.com/questions/30299671/matching-strings-with-wildcard) – Wiktor Stribiżew Feb 09 '17 at 07:28
  • @JonSkeet I tried out Contains Method,that is already shared.But after having % sign , it consider it as a part of string , not a wildcard character. – Ehsan Ullah Nazir Feb 09 '17 at 07:29
  • @un-lucky It should return true in both cases , if i compare Str1 with Str2 or vice versa – Ehsan Ullah Nazir Feb 09 '17 at 07:31
  • 3
    Yes, `Contains` doesn't use regular expressions - I'm not sure why you'd think it would. Have you done any research on using regular expressions in .NET? (Stack Overflow should be a *last* resort after doing all the research you can, not your first port of call after the very first thing you try doesn't work.) – Jon Skeet Feb 09 '17 at 07:31
  • @JonSkeet right – Ehsan Ullah Nazir Feb 09 '17 at 07:39
  • im not sure what you are trying to do... sound like you want it to check each thing after a [space], i would suggest then just programming it like that instead of adding in confusing sql aka % – Seabizkit Feb 09 '17 at 07:52
  • ie `But what my requirement is , if we compare str2 and str3 , it should return True , as it lies in Str2.` is not true or badly writen – Seabizkit Feb 09 '17 at 07:54

1 Answers1

6

I suggest converting SQL-LIKE into regular expression:

private static string LikeToRegular(string value) {
  return "^" + Regex.Escape(value).Replace("_", ".").Replace("%", ".*") + "$";
}

And then use Regex as usual:

string like = "%A%C%";
string source = "A B C";

if (Regex.IsMatch(source, LikeToRegular(like))) {
  Console.Write("Matched");
}

You can even implement an extension method if you want:

public class StringExtensions {
  public static bool ContainsLike(this string source, string like) {
    if (string.IsNullOrEmpty(source))
      return false; // or throw exception if source == null
    else if (string.IsNullOrEmpty(like))
      return false; // or throw exception if like == null 

    return Regex.IsMatch(
      source,
      "^" + Regex.Escape(like).Replace("_", ".").Replace("%", ".*") + "$");
  }
}

So you can put

string like = "%A%C%";
string source = "A B C";

if (source.ContainsLike(source, like)) {
  Console.Write("Matched"); 
} 
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 2
    One remark: if the string is too long, and the pattern contains a lot of `%` wildcards, the `.*` pattern might slow down code execution. Maybe the best way is to use [Tim's `LikeOperator.LikeString` approach](http://stackoverflow.com/a/30299838/3832970) (not tested though). – Wiktor Stribiżew Feb 09 '17 at 07:42
  • Since you created an extension method, you have to omit the first parameter "source" in the parentheses of "ContainsLike" (in your last example code) – gooleem Jul 21 '20 at 13:31