简体   繁体   中英

How do I find if a string(A) exists in another string(B) using c#?

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"?

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.

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);

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

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.

  1. I applied the " Longest Common Substring " algorithm and founded the longest common substring between the two strings.

  2. Then I used "Levenshtein Distance algorithm" to compare my String A with the "Longest Common Substring" found from step 1.

  3. 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.

  4. Problem Solved .

I have worked on the problem for one day and I have found decent results for the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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