tags:

views:

1059

answers:

7

I need help with creating a C# method that returns the index of the Nth occurance of a character in a string.

For instance, the 3rd occurance of the character 't' in the string "dtststx" is 5.

+1  A: 

Update: Index of Nth occurance one-liner:

int NthOccurence(string s, char t, int n)
{
    s.TakeWhile(c => n - (c == t)?1:0 > 0).Count();
}

Use these at your own risk. This looks like homework, so I left a few bugs in there for your to find:

int CountChars(string s, char t)
{
   int count = 0;
   foreach (char c in s)
      if (s.Equals(t)) count ++;
   return count;
}

.

int CountChars(string s, char t)
{
     return s.Length - s.Replace(t.ToString(), "").Length;
}

.

int CountChars(string s, char t)
{
    Regex r = new Regex("[\\" + t + "]");
    return r.Match(s).Count;
}
Joel Coehoorn
+2  A: 

Joel's answer is good (and I upvoted it). Here is a LINQ-based solution:

yourString.Where(c => c == 't').Count();
Andrew Hare
@Andrew - you can shorten this by skipping the `Where` and passing the predicate to the `Count` method. Not that there is anything wrong with the way it is.
Mike Two
@Mike - Good point!
Andrew Hare
+3  A: 
int count = s.Count( c => c == 't' );

where s is a string

Edit

After a comment by the OP I think you mean the index of the nth occurrence.

public int GetNthIndex(string s, char t, int n)
{
    int count = 0;
    for (int i = 0; i < s.Length; i++)
    {
        if (s[i] == t)
        {
            count++;
            if (count == n)
            {
                return i;
            }
        }
    }
    return -1;
}

That could be made a lot cleaner, and there are no checks on the input.

Mike Two
A: 

Here's another LINQ solution:

string input = "dtststx";
char searchChar = 't';
int occurrencePosition = 3; // third occurrence of the char
var result = input.Select((c, i) => new { Char = c, Index = i })
                  .Where(item => item.Char == searchChar)
                  .Skip(occurrencePosition - 1)
                  .FirstOrDefault();

if (result != null)
{
    Console.WriteLine("Position {0} of '{1}' occurs at index: {2}",
                        occurrencePosition, searchChar, result.Index);
}
else
{
    Console.WriteLine("Position {0} of '{1}' not found!",
                        occurrencePosition, searchChar);
}

Just for fun, here's a Regex solution. I saw some people initially used Regex to count, but when the question changed no updates were made. Here is how it can be done with Regex - again, just for fun. The traditional approach is best for simplicity.

string input = "dtststx";
char searchChar = 't';
int occurrencePosition = 3; // third occurrence of the char

Match match = Regex.Matches(input, Regex.Escape(searchChar.ToString()))
                   .Cast<Match>()
                   .Skip(occurrencePosition - 1)
                   .FirstOrDefault();

if (match != null)
    Console.WriteLine("Index: " + match.Index);
else
    Console.WriteLine("Match not found!");
Ahmad Mageed
+1  A: 

Here is a fun way to do it

     int i = 0;
     string s="asdasdasd";
     int n = 3;
     s.Where(b => (b == 'd') && (i++ == n));
     return i;
Dested
A: 

Another RegEx-based solution (untested):

int NthIndexOf(string s, char t, int n) {
   if(n < 0) { throw new ArgumentException(); }
   if(n==1) { return s.IndexOf(t); }
   if(t=="") { return 0; }
   string et = RegEx.Escape(t);
   string pat = "(?<="
      + Microsoft.VisualBasic.StrDup(n-1, et + @"[.\n]*") + ")"
      + et;
   Match m = RegEx.Match(s, pat);
   return m.Success ? m.Index : -1;
}

This should be slightly more optimal than requiring RegEx to create a Matches collection, only to discard all but one match.

richardtallent
In response to the Matches collection comment (since that is what I had shown in my response): I suppose a more efficient approach would be to use a while loop checking for `match.Success` and get the `NextMatch` while incrementing a counter and breaking early when the `counter == index`.
Ahmad Mageed
A: 
    public static int FindOccuranceOf(this string str,char @char, int occurance)
    {
       var result = str.Select((x, y) => new { Letter = x, Index = y })
            .Where(letter => letter.Letter == @char).ToList();
       if (occurence > result.Count || occurance <= 0)
       {
           throw new IndexOutOfRangeException("occurance");
       }
       return result[occurance-1].Index ;
    }
Islam Ibrahim