String.IndexOf Method
Reports the zero-based index of the first occurrence of one or more characters, or the first occurrence of a string, within this string. The method returns -1 if the character or string is not found in this instance.
This member is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list.
| Name | Description | |
|---|---|---|
|
IndexOf(Char) | Reports the zero-based index of the first occurrence of the specified Unicode character in this string. |
|
IndexOf(String) | Reports the zero-based index of the first occurrence of the specified string in this instance. |
|
IndexOf(Char, Int32) | Reports the zero-based index of the first occurrence of the specified Unicode character in this string. The search starts at a specified character position. |
|
IndexOf(String, Int32) | Reports the zero-based index of the first occurrence of the specified string in this instance. The search starts at a specified character position. |
|
IndexOf(String, StringComparison) | Reports the zero-based index of the first occurrence of the specified string in the current String object. A parameter specifies the type of search to use for the specified string. |
|
IndexOf(Char, Int32, Int32) | Reports the zero-based index of the first occurrence of the specified character in this instance. The search starts at a specified character position and examines a specified number of character positions. |
|
IndexOf(String, Int32, Int32) | Reports the zero-based index of the first occurrence of the specified string in this instance. The search starts at a specified character position and examines a specified number of character positions. |
|
IndexOf(String, Int32, StringComparison) | Reports the zero-based index of the first occurrence of the specified string in the current String object. Parameters specify the starting search position in the current string and the type of search to use for the specified string. |
|
IndexOf(String, Int32, Int32, StringComparison) | Reports the zero-based index of the first occurrence of the specified string in the current String object. Parameters specify the starting search position in the current string, the number of characters in the current string to search, and the type of search to use for the specified string. |
The documentation says it should throw an ArgumentOutOfRangeException if I use it like this (because the startIndex of 5 is not valid for the string "abcde"):
string s = "abcde";
int index = s.IndexOf("fgh", 5);
It doesn't throw the exception. (Every now and then it DOES throw the exception, but I can't reproduce the exception consistently so I suppose it must have something to do with the underlying implementation.)
(Or, maybe I'm misunderstanding something. Let me know.)
When Is ArgumentOutOfRangeException Thrown?
The IndexOf(Char, Int32), IndexOf(String.Int32), and IndexOf(String, Int32, StringComparison) overloads all throw an ArgumentOutOfRangeException if the startIndex parameter is greater than the string's length. In other words, startIndex can be one greater than the last character position in the string without an exception being thrown.
The IndexOf(Char, Int32, Int32), IndexOf(String, Int32, Int32), and IndexOf(String, Int32, Int32, StringComparison) overloads similarly allow startIndex to range from 0 to String.Length, or one greater than the last index position in the string instance. However, count cannot exceed String.Length minus startIndex. In other words, startIndex can still identify a position one character beyond the string, but the attempt to search from that position by supplying any non-zero positive value results in an ArgumentOutOfRangeException.
We'll revise the documentation to make this clear. I hope that this helps to remove some of the confusion about when the exception is thrown.
--Ron Petrusha
Common Language Runtime User Education
Microsoft Corporation
- 3/1/2011
- TonySeattle
- 11/2/2011
- R Petrusha - MSFT
Yes, as pointed out by the previous post, the behavior is not described very clearly.
String.IndexOf(string searchstring, int startPos) throws an exception when startPos > length, but returns -1 when startPos = length
> "ab".IndexOf("b", 2);;
val it : int = -1
> "ab".IndexOf("b", 3);;
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Note the exception string is inaccurate: it should state: less than or equal to the size of the collection.
This is for .NET 4.0 (VS 2010 SP1).
- 4/6/2011
- clearstar