Reputation: 2244
Before marking this as duplicate, please read the details here.
Example 1:
String A: The seven habits of highly effective people.
String B: "This is a sample text. There is only one product in it. It is a book. The book is The seven habits of highly effective people."
Example 2:
String A: The seven habits of highly effective people.
String B: "This is a sample text. There is only one product in it. It is a book. The book is The seven habits of highly effective peopl."
Now solving the above examples with a code like
B.Contains(A)
will give the correct results. However the same code will return "false" as output in Example 2.
How do I resolve this problem?
There is an "e" missing in example 2 and I am aware about it and that's the problem. How do I compare one string with another where string A is nearly identical with a "part of string B"?
Upvotes: 1
Views: 174
Reputation: 2244
I am answering my own question.
I was looking for a solution to compare one string with another where string A is nearly identical with a "part of string B".
This is how I resolved the issue.
I applied the "Longest Common Substring" algorithm and founded the longest common substring between the two strings.
Then I used "Levenshtein Distance algorithm" to compare my String A with the "Longest Common Substring" found from step 1.
If the result available from the algorithm mentioned in step 2 is above certain threshold, then it implies that the string A exists in String B.
Problem Solved.
I have worked on the problem for one day and I have found decent results for the problem.
Upvotes: 0
Reputation: 76
You can use string.compare
, Find below few examples which may help you.
string a = "a";
string b = "b";
int c;
c = string.Compare(a, b);
Console.WriteLine(c);
c = string.CompareOrdinal(b, a);
Console.WriteLine(c);
c = a.CompareTo(b);
Console.WriteLine(c);
c = b.CompareTo(a);
Console.WriteLine(c);
Upvotes: 1
Reputation: 2949
What you are looking for looks like a search engine with score rate.
I used the Levenshtein Distance methode to search/compare string that looks like the same but who are not.
there is an example at the following link :
http://www.dotnetperls.com/levenshtein
Upvotes: 0
Reputation: 65079
As stated in my comment.. the Levenshtein Distance algorithm (and similar ones) compute differences between strings and return a numerical result (wiki: http://en.m.wikipedia.org/wiki/Levenshtein_distance).
However, I would definitely apply benchmarking and caching strategies for these algorithms. They are decent with small input.. but when I have implemented it I have had to make sure I cache results / lookups. Your large example will not perform "fast".. depending on what "fast" is for your use case.
Upvotes: 2