开发者

Linq two strings searching for same combination

I have two strings

开发者_运维技巧
string a="1,2,3";

string b="2,4,6,7,8"

I want to search for the same number in both the strings. What is the best way to do it in LINQ?


This example compare it string-wise though:

var chars = 
    from n1 in a.Split(',')
    join n2 in b.Split(',') on n1 equals n2
    select n1;


If you really want to compare the numbers, it is far easier to split the strings and use an extension method like Enumerable.Intersect. If you want a more efficient method, you should look for implementations for retrieving the largest common substring between the two strings, like the one in Wikipedia. This way you avoid the cost of splitting and possible inefficiencies in the Enumerable.Intersect implementation. You will certainly find even more efficient implementations of LCS if you search.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜