Java indexOf 为未找到的字符串返回false?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/696809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java indexOf returns false for unfound string?
提问by Supernovah
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?
我需要知道在 Java 中该indexOf()
方法是返回false
还是返回void
未找到的字符串?还是返回索引int
为 0?
回答by Brian Agnew
回答by Thomas Owens
The Java API docscontain this answer. the indexOf
methods on a String
return -1 if the character is not found.
在Java的API文档包含这个答案。如果未找到字符,则返回 -1的indexOf
方法String
。
回答by Gustavo
You mean Javascript? It returns -1.
你的意思是Javascript?它返回-1。
回答by Brian Rasmussen
回答by scunliffe
Only PHP's str_posis weird enough to return 0/false when the index isn't found. Most consider the PHP version to be a bad implementation.
只有PHP 的 str_pos很奇怪,当找不到索引时返回 0/false 。大多数人认为 PHP 版本是一个糟糕的实现。
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.
*/