I need to know whether in Java does the indexOf()
method return false
or void
for an unfound string? or does it return an index int
of 0?
views:
1210answers:
5"Try it" isn't particularly definitive - but "read the docs" is :)
Jon Skeet
2009-03-30 11:25:14
Assuming the docs are correct :-) I take your point, however.
Brian Agnew
2009-03-30 11:38:25
+5
A:
The Java API docs contain this answer. the indexOf
methods on a String
return -1 if the character is not found.
Thomas Owens
2009-03-30 11:21:43
No, he means Java, hence the phrase 'in Java', rather than 'in Javascript'
Visage
2009-03-30 11:24:13
It is a very basic question, so his knowledge is very limited. I heard people refering to Javasctipt as Java when they are begginers. Now he knows the answer for two different questions ;)
Gustavo
2009-03-30 11:28:54
+3
A:
Look at the signature. It says int
, so an integer is returned. To return another type (void or boolean) the signature would be different.
Brian Rasmussen
2009-03-30 11:23:50
I'm guessing he is coming from the PHP world, where the "int" datatype is "only a guideline" ;-)
scunliffe
2009-03-30 13:06:19
+2
A:
Only PHP's str_pos is weird enough to return 0/false when the index isn't found. Most consider the PHP version to be a bad implementation.
int strpos ( string $haystack , mixed $needle [, int $offset= 0 ] )
//Returns the position as an integer. If needle is not found, strpos()
// will return boolean FALSE.
/*
Warning
function may return Boolean FALSE, but may also return a non-Boolean value which
evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more
information. Use the === operator for testing the return value of this function.
*/
scunliffe
2009-03-30 13:04:04
What I don't get is the... ok so if the string starts with the keyword you are looking for, it will return 0, but if doesn't find what you are looking for, it might also return 0? WT?
scunliffe
2009-03-30 18:04:39
@scunliffe: Although 0 == false, 0 !== false. But indeed, that's one of the worse design decisions in PHP's built-in functions (as in: bites me in the butt every so often).
Piskvor
2010-05-04 09:21:19