String.LastIndexOfAny throwing an odd exception

I am running into an unexpected (on my behalf) exception using String.LastIndexOfAny(char[], int, int) that I can't seem to figure out. The following code will replicate this by throwing an ArgumentOutOfRangeException, "Count [3rd variable] must be positive and count must refer to a location within the string/array/collection.", when I call test.LastIndexOfAny(anyOf, start, count) even though "count" will pass if I test for boundaries (0, test.Length) prior to this call:

string test = "chicken nugget";
char[] anyOf = { ' ', 'e' };
int start = 0;
int count = 10;
int lastIndex = test.LastIndexOfAny(anyOf, start, count);

Anyone have any ideas where I am going wrong using this function I am using this on a TextBox .Text to find the last index of a list of chars (anyOf) anywhere prior to the cursor (count) ultimately to recreate Ctrl-Back functionality so suggestions to recreate that behavior is also helpful.


Answer this question

String.LastIndexOfAny throwing an odd exception

  • rr12

    "This method begins searching at the startIndex character position of this instance and proceeds backwards towards the beginning until either a character in anyOf is found or count character positions have been examined."

    so. . . wouldnt you want:

    int lastIndex = test.LastIndexOfAny(anyOf, count-1, count);


     



  • String.LastIndexOfAny throwing an odd exception