繁体   English   中英

Java indexOf对于找不到的字符串返回false?

[英]Java indexOf returns false for unfound string?

我需要知道在Java中, indexOf()方法是否为未找到的字符串返回falsevoid 还是返回索引int 0?

它将返回-1(提示:试试看)

就像在JavaDoc中一样

Java API文档包含此答案。 如果找不到字符,则String上的indexOf方法返回-1。

签名 它说int ,所以返回一个整数。 要返回另一种类型(无效或布尔值),签名将有所不同。

只有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.
*/

您是说Javascript吗? 返回-1。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM