Wednesday, July 06, 2005 - 12:05

string.LastIndexOf

Okay, here's one that had me stuck for a while this morning, until I realized what was going on.

String.LastIndexOf(char, index) just didn't seem to be finding the char in the string, even though it was clearly there. It just didn't work.

So I tried String.IndexOf, and that worked ....

and then it came to me in a flash of memory: I've seen this before :-) LastIndexOf actually works in reverse, so the index is the first position you want to start the search from, not the first position in the string - i.e. if you want to search the entire string, index will be string.length-1, not 0. It's obvious once you realise it, but the docs don't mention it, so I thought I'd mention it here as a heads-up.

And maybe I'll remember it next time ....

Labels:

0 Comments:

Post a Comment

<< Home