Javascript 为什么 IndexOf 返回 -1?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8585897/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:35:51  来源:igfitidea点击:

Why does IndexOf return -1?

javascriptindexof

提问by Adnan Khan

I am learning Javascript and don't understand why the indexOf below returns -1:

我正在学习 Javascript 并且不明白为什么下面的 indexOf 返回 -1:

var string = "The quick brown fox jumps over the lazy dog";

console.log (string.indexOf("good"));

回答by Interrobang

-1 means "no match found".

-1 表示“未找到匹配项”。

The reason it returns -1 instead of "false" is that a needle at the beginning of the string would be at position 0, which is equivalent to false in Javascript. So returning -1 ensures that you know there is not actually a match.

它返回 -1 而不是 "false" 的原因是字符串开头的针将位于位置 0,这相当于 Javascript 中的 false。因此,返回 -1 可确保您知道实际上没有匹配项。

回答by mpen

-1 means no match found. "good" is not in that sentence. This is documentedbehaviour.

-1 表示未找到匹配项。“好”不在那个句子中。这是记录在案的行为

The indexOf()method returns the first index at which a given element can be found in the array, or -1if it is not present.

indexOf()方法返回可以在数组中找到给定元素的第一个索引,或者-1如果它不存在。

回答by Mauvis Ledford

Because arrays are 0 based, returning 0 would mean starting from the first character was matched; 1, the second character, and so on. This means anything 0 and up would be a true or "found" response. To keep everything in the integer category, -1 signifies no match found.

因为数组是基于 0 的,所以返回 0 意味着从匹配的第一个字符开始;1、第二个字符,依此类推。这意味着 0 及以上的任何内容都是真实或“找到”的响应。要将所有内容保留在整数类别中,-1 表示未找到匹配项。

回答by VietNg

There is another reason for indexOf to return -1 when no match is found. Consider the code below:

indexOf 在找不到匹配项时返回 -1 的另一个原因。考虑下面的代码:

if (~str.indexOf(pattern)){
  console.log('found')
}else{
  console.log('not found')
}

Because ~(-1) = 0 so the fact that indexOf returning -1 makes it easier to write if...else using ~.

因为 ~(-1) = 0 所以 indexOf 返回 -1 的事实使得使用 ~ 编写 if...else 变得更容易。

回答by farzadgo

Related issue; even if the subject word exists in the sentence (semantically speaking) or any sequence of letters matching partially, indexOf() would return the index of its first matching letter. e.g.

相关问题;即使主题词存在于句子中(从语义上讲)或任何部分匹配的字母序列,indexOf() 也会返回其第一个匹配字母的索引。例如

const string = "The quick brown fox jumps over the lazy dog";
console.log(string.length);
// expected output: 43

console.log (string.indexOf("fox"));
// expected output: 16

console.log (string.indexOf(" q"));
// expected output: 3

But if the partial sequence of letters is not matching any part of the string array, the method will return -1, the same case with "good";

但是如果部分字母序列与字符串数组的任何部分都不匹配,则该方法将返回-1,与“good”的情况相同;

console.log (string.indexOf("foxy"));
// expected output: -1

console.log (string.indexOf("good"));
// expected output: -1

Just in case, turning it to an array of sub-strings (here the words) is possible with split() method, splitting the long string by its spaces (punctuations);

以防万一,可以使用 split() 方法将其转换为子字符串数组(此处为单词),将长字符串按空格(标点符号)拆分;

const words = string.split(' ');
console.log(words);
// expected output: ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]

console.log(words.indexOf("fox"));
// expected output: 3

回答by numegil

The search never finds what it's looking for ("good" isn't in the sentence), and -1 is the default return value.

搜索永远找不到它要找的东西(“good”不在句子中),-1 是默认返回值。

http://www.w3schools.com/jsref/jsref_indexof.asp

http://www.w3schools.com/jsref/jsref_indexof.asp