6

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 this question I have Googled a lot, but have not found easy solution to solve this simple problem.

1
  • Can the instances of the substring overlap? Commented Apr 20, 2009 at 10:53

4 Answers 4

16

How about this extension method:

public static IEnumerable<int> IndexesOf(this string haystack, string needle)
{
    int lastIndex = 0;
    while (true)
    {
        int index = haystack.IndexOf(needle, lastIndex);
        if (index == -1)
        {
            yield break;
        }
        yield return index;
        lastIndex = index + needle.Length;
    }
}

Note that when looking for "AA" in "XAAAY" this code will now only yield 1.

If you really need an array, call ToArray() on the result. (This is assuming .NET 3.5 and hence LINQ support.)

Sign up to request clarification or add additional context in comments.

3 Comments

Could you please adjust it, so that it would return only 1
instead of advancing by 1, advance by the length of the needle -- to use Jon's nomenclature :)
Exactly - but it can be slightly tricky to work out exactly where to do that :) I've fixed the code.
6
    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<int>();
          }
    }

1 Comment

How would I do it, if I needed chChar to be string? Thank You.
6

You would have to loop, I suspect:

        int start = 0;
        string s = "abcdeafghaji";
        int index;
        while ((index = s.IndexOf('a', start)) >= 0)
        {
            Console.WriteLine(index);
            start = index + 1;
        }

Comments

2

Using a solution that utilizes regex can be more reliable, using the indexOf function can be unreliable. It will find all matches and indexes, not matching an exact phrase which can lead to unexpected results. This function resolves that by making use of the Regex library.

public static IEnumerable<int> IndexesOf(string haystack, string needle)
{
       Regex r = new Regex("\\b(" + needle + ")\\b");
       MatchCollection m = r.Matches(haystack);

       return from Match o in m select o.Index;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.