Finding multiple indexes from source string

后端 未结 4 1589
眼角桃花
眼角桃花 2021-01-12 21:23

Basically I need to do String.IndexOf() and I need to get array of indexes from the source string.

Is there easy way to get array of indexes?

Before asking t

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-12 21:47

        var indexs = "Prashant".MultipleIndex('a');
    
    //Extension Method's Class
        public static class Extensions
        {
             static int i = 0;
    
             public static int[] MultipleIndex(this string StringValue, char chChar)
             {
    
               var indexs = from rgChar in StringValue
                            where rgChar == chChar && i != StringValue.IndexOf(rgChar, i + 1)
                            select new { Index = StringValue.IndexOf(rgChar, i + 1), Increament = (i = i + StringValue.IndexOf(rgChar)) };
                i = 0;
                return indexs.Select(p => p.Index).ToArray();
              }
        }
    

提交回复
热议问题