日期:2014-05-18  浏览次数:21139 次

如果判断多行字符串共同含有什么子字符串(该子字符串须长度最长的)
C# code

abcdefgh
abcdefg
xbcdefe
...
aaabcdefgh



比如上面的多行字符串,含有共同的子字符串,须长度最长的子字符串是:"bcdef"....C#怎么实行吗?

------解决方案--------------------
先找出最短的字串,然后从这个字串截取length,length-1,...,1长度的子串,直到挑出符合条件的为止
------解决方案--------------------
http://blog.csdn.net/zztfj/article/details/6157429
------解决方案--------------------
C# code

string[] str = new string[]{ "gfdsgaaaa", "fds", "fdsgfsdfds", "fdsagfdserre", "rfvffgfdssss" };
var query = str.Where(w => w.Contains("fds"));
var result = from w in query where w.Length == query.Max(m => m.Length)
             select w;
foreach (var s in result)
    Console.WriteLine(s);

------解决方案--------------------
C# code

            string[] str = new string[] { "gfdsgaaaa", "fds", "fdsgfsdfds", "fdsagfdserre", "rfvffgfdssss" };
            var query = str.Where(w => w.Contains("fds"));
            var result = query.Where(w => w.Length == query.Max(m => m.Length));
            foreach (var s in result)
                Console.WriteLine(s);
            /** result print:
             * fdsagfdserre 
             * rfvffgfdssss
             * */