0

I am working with a C# Windows form application. Can anyone help me with the fastest way to detect if more than 'x' number of alphabets of string one are present in string two in same pattern & vice versa.

Example: "dog" in "puppydogphotos.jpg" would count as a detection.

I am doing something like below but its failing on some instances:

foreach (var word in Kewords.Split(','))
{
    var filename = Path.GetFileNameWithoutExtension(e.FullPath).ToLower();
    var extenion = Path.GetExtension(e.FullPath).ToLower();

    if (word.ToLower().Contains(filename) || word.ToLower().StartsWith(filename) || word.ToLower().EndsWith(filename) || word.Contains(extenion) || filename.ToLower().Contains(word.ToLower()) ||filename.ToLower().StartsWith(word) || filename.ToLower().EndsWith(word))
    {  
      keywordMatch = true;
    }
} 

Thanks

Halvor Holsten Strand
  • 19,829
  • 17
  • 83
  • 99
Robin
  • 283
  • 4
  • 22
  • 1
    Can you explain more clearly what you are trying to detect? Surely it should be filename.contains(word) rather than word.contains(filename) – Alex K. Dec 21 '15 at 16:34
  • Let's improve the things... This `if` statement looks very long and horrible! You don't need to check for `StartsWith` or `EndsWith` if you use `Contains` and ||. Also, just convert using `ToLower` once and then call `Contains`, because it will have the same effect as the whole statement. So all you need is the first condition in your if. – Alex Dec 21 '15 at 16:38
  • Ok, Case 1: suppose string 1 has "verybaddata.jpg" & string 2 has "bad" or "dda", I want to return a true. Case 2: suppose string 1 has "trex.txt" & string 2 has "ex.tx" or "rex", I want to return a true. (true means that more than 2 characters in sequence are shared by two string variables). – Robin Dec 21 '15 at 16:39
  • @Robin all you need in this case is `ToLower` and `Contains`. You don't need to check anything else. Furthermore you don't need `if` statement. All you need to return is `word.ToLower().Contains(filename)` which will tell you whether it's true or false. – Alex Dec 21 '15 at 16:41

1 Answers1

0

Looks like method

    String.Intersect

should be able to help you.

Small example here: Implementing an efficent algorithm to find the intersection of two strings

Community
  • 1
  • 1
Imaver
  • 18
  • 3