日期:2014-05-17  浏览次数:20475 次

牛人谁能解答 string[]数组和string的问题
有个数组 string[] s1={"a","b","c","d","e","f","h","ab","bc","abf","eh","bh","bcf","ch"}

还有一个 string s2= "cbeh"  

想得到的结果是数组 s3={"b","c","e","h","bc","bh","eh","bh","ch"}

这个数组s1和s2的值都是变化的也有可能是其他值,但规则是这样的。

就是想得到s1中包含s2中的数组。s2的值不一定是按a-z的顺序排列

这个我实在想不出有什么好办法,之前用了s1.Where(i => s2.Contains(i)) 但是遇到数据不连续的就失效了

谢谢~~

------解决方案--------------------

 static void Main(string[] args)
        {
            string[] s1 = { "a", "b", "c", "d", "e", "f", "h", "ab", "bc", "abf", "eh", "bh", "bcf", "ch" };
            string s2 = "cbeh";


            foreach (string s in s1)
            {
                if (IsVal(s, s2))
                    System.Console.WriteLine(s);
            }
            System.Console.Read();
        }


        private static bool IsVal(string s,string str)
        {
            bool result = true;

            for (int i = 0; i < s.Length; i++)
            {
                //System.Console.WriteLine(s.Substring(i, 1));
                if (str.IndexOf(s.Substring(i, 1)) < 0)
                    result = false;
            }
            return result;
        }

目前这样尝试着实现了一下。。可能有点麻烦,不过功能貌似实现了,不知道是不是LZ需要的。
------解决方案--------------------
引用:
这个写的转换成2进制的  还是有点不太明白。"a","b","c","e","ab","ac","abe"转换出的2进制结果能和"ac" 二进制结果对比 得出 "a","c","ac"吗 


class Program
    {
        static void Main(string[] args)
        {
            
             string[] s1={"a","b","c","d","e","f","h","ab","bc","abf","eh","bh","bcf","ch"};
             s