其中字符串的那个怎么把T约束成String和char,不能约束?目测这两个方法就比较慢,求大神优化。
public static class ExtendHelper
    {
        /// <summary>
        /// Find some indexes of subString in a long string
        /// </summary>
        /// <param name="_input"></paramc
        /// <param name="_lookfor"></param>
        /// <param name="start"></param>
        /// <returns></returns>
        public static int[] FindIndexes(this String _input, String _lookfor, int start = 0)
        {
            List<int> result = new List<int>();
            int Start = start < 0 ? 0 : start;
            int current = _input.IndexOf(_lookfor, Start);
            while (!current.Equals(-1))
            {
                result.Add(current);
                current = _input.IndexOf(_lookfor, current + 1);
            }
            return result.ToArray();
        }        //public static int[] FindIndexes(this String _input, char _lookfor, int start = 0)
        //{
        //    List<int> result = new List<int>();
        //    int Start = start < 0 ? 0 : start;
        //    int current = _input.IndexOf(_lookfor, Start);
        //    while (!current.Equals(-1))
        //    {
        //        result.Add(current);
        //        current = _input.IndexOf(_lookfor, current + 1);
        //    }
        //    return result.ToArray();
        //}        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="t"></param>
        /// <param name="start"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        static public IEnumerable<int> FindIndexes<T>(this List<T> list, T t, int start = 0, int count = 1) where T : IComparable<T>
        {
            int actualNum = 0;
            int Count = count > list.Count ? list.Count : count;
            int Start = start < 0 ? 0 : start;            int current = list.FindIndex(Start, v => v.CompareTo(t) == 0);            while (!current.Equals(-1) && actualNum < Count)
            {
                if (actualNum != 0)
                    current = list.FindIndex(current + 1, v => v.CompareTo(t) == 0);                if (current.Equals(-1))
                    break;                yield return current;
                actualNum++;
            }
        }
    }

解决方案 »

  1.   

    static public IEnumerable<int> FindIndexes<T>(this List<T> list, T t, int start = 0, int count = 1) where T : IComparable<T>
            {
                return list.Select((x, i) => new { x, i }).Skip(start).Take(count).Where(x => x.x.CompareTo(t) == 0).Select(x => x.i);            
            }
      

  2.   

    字符串匹配的那个自己google kmp算法。
      

  3.   

    这么简单的算法性能应该不会有大的提高了,建议
    1 用最基本的循环,用 ()=> 会多一层函数调用
    2 整数比较就别用 Equals 了。
    有个错误:第一个方法查找下一个时不该用 current+1,应该用current+_lookfor.length,并且判断 _lookfor长度不为0.
      

  4.   

    不明觉厉呀,对Skip和Take两个方法不熟,准备研究下,但结果好像有点问题
      

  5.   

    你说的比较有道理,其实我一直在疑惑一个问题:.NET包装过的Select和Lambada结合起来用是不是比完全自己实现快很多;current+_lookfor.length这个虽然看似精确,但是有问题 比如“baaaaaa”.FindIndexes("aa")
      

  6.   

    什么问题。比如
    12345
    从2找起,找3,结果是{1}还是{3}
    如果是前者,得这么写:
    return list.Skip(start).Take(count).Select((x, i) => new { x, i }).Where(x => x.x.CompareTo(t) == 0).Select(x => x.i);
      

  7.   

    什么问题。比如
    12345
    从2找起,找3,结果是{1}还是{3}
    如果是前者,得这么写:
    return list.Skip(start).Take(count).Select((x, i) => new { x, i }).Where(x => x.x.CompareTo(t) == 0).Select(x => x.i); List<String> al = new List<string> { "0", "8", "1", "2", "3", "4", "8", "5", "6", "7", "8", "7", "8" };
                al.FindIndexes("8", 0, 3).ToList().ForEach(x => Console.WriteLine(x));只给我返回1,我希望返回三个Index呀
      

  8.   

    可能我的start描述不清楚,我想把它作为一个查找的起始位置,不是元素内容:
    我改了你的:return list.Select((x, i) => new { x, i }).Where(x => x.x.CompareTo(t) == 0 && x.i >= start).Select(x => x.i).Take(count);