PDA

View Full Version : string.indexOf()


kentyler
04-25-2003, 06:08 AM
Good Morning,
It looks like String.indexOf() works, but it does not respect a start position. It returns the index starting from the begining of the string regardless of what start position is entered.

<canvas debug="true">
<script>
<![CDATA[
x = new string("now is the time"); // returns a string object
debug.write("'is' starts in the " + x.indexOf("is") + " postion in the string " + x );
debug.write("'is' starts in the " + x.indexOf("is", 2) + " postion in the string " + x + " if we start looking from the 2nd postion" );
]]>
</script>
</canvas>

antun
04-25-2003, 06:54 AM
That's because start position means refers to the point you want to start searching for the substring. indexOf() will always return the distance of the first occurence of the substring from the start of the string. The optional start argument is used to keep searching the rest of the string.

-Antun

kentyler
04-25-2003, 12:49 PM
my reference says
Returns
The position of the first occurrence of substring with strin gthat appears AFTER THE START POSITION [my empahsis], I wonder what the formal EMCA262 standard says ?

antun
04-25-2003, 02:11 PM
Here's exactly what the ECMAScript standard says:


String.prototype.indexOf (searchString, position)
If searchString appears as a substring of the result of converting this object to a string, at one or more
positions that are greater than or equal to position, then the index of the smallest such position is
returned; otherwise, -1 is returned. If position is undefined, 0 is assumed, so as to search all of the
string.
The indexOf method takes two arguments, searchString and position, and performs the following
steps:
[list=1]
Call ToString, giving it the this value as its argument.
Call ToString(searchString).
Call ToInteger(position). (If position is undefined, this step produces the value 0).
Compute the number of characters in Result(1).
Compute min(max(Result(3), 0), Result(4)).
Compute the number of characters in the string that is Result(2).
Compute the smallest possible integer k not smaller than Result(5) such that k+Result(6) is not
greater than Result(4), and for all nonnegative integers j less than Result(6), the character at
position k+j of Result(1) is the same as the character at position j of Result(2); but if there is no
such integer k, then compute the value -1.
Return Result(7).
[/list=1]
The length property of the indexOf method is 1.
NOTE
The indexOf function is intentionally generic; it does not require that its this value be a String
object. Therefore, it can be transferred to other kinds of objects for use as a method.


I'm really not 100% sure which is correct, but both make sense.

-Antun

antun
04-25-2003, 02:24 PM
Upon re-reading this I wondered if there was anything there that suggested that the measurement of the position started with the start position, and I'm beginning to think that it didn't.

Next I tried out the method in client-side JavaScript in Internet Explorer 6, Netscape 4.7 and 7:


<script>
var foo = "This is my string";
document.write( foo.indexOf( 'is', 4 ) );
</script>


They all output 5, which is the index from the start of the string.

-Antun


Originally posted by kentyler
my reference says
Returns
The position of the first occurrence of substring with string that appears AFTER THE START POSITION [my empahsis], I wonder what the formal EMCA262 standard says ?