tags:

views:

87

answers:

1
int d;
d = some_string.IndexOf("something",1000);

I want indexOf to search some_string, starting at position 1000 and searching backwards. is this possible?

+7  A: 

How about LastIndexOf?

From MSDN:

Reports the index position of the last occurrence of a specified String within this instance...The search begins at the startIndex character position of this instance and proceeds backwards towards the beginning until either value is found or the first character position has been examined.

int index = some_string.LastIndexOf("something", 1000);
Zach Johnson
You beat me to it!
Nix
`some_string.LastIndexOf("something", 0, 1000)` to be precise. http://msdn.microsoft.com/en-us/library/d0z3tk9t%28v=VS.80%29.aspx
Justin Johnson
@Zach It seems to me that he wants to search the range of [0, 1000]
Justin Johnson
@Justin: The starting index is the index from which to proceed *backward*, which makes it confusing and unintuitive compared to `IndexOf`. So actually it would be `some_string.LastIndexOf("something", 1000, 1000)`. I've been bitten by this one before.
Zach Johnson
Peculiar indeed.
Justin Johnson