From 01b0e12ba27b5069248fd09235e9a7143915ee30 Mon Sep 17 00:00:00 2001 From: Irvin Date: Wed, 16 Feb 2022 02:02:49 +0800 Subject: remove `notranslate` class in zh-CN --- .../reference/global_objects/string/indexof/index.html | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'files/zh-cn/web/javascript/reference/global_objects/string/indexof') diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/indexof/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/indexof/index.html index 21434132e5..80213be40f 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/string/indexof/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/string/indexof/index.html @@ -22,7 +22,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/indexOf

语法

-
str.indexOf(searchValue [, fromIndex])
+
str.indexOf(searchValue [, fromIndex])

参数

@@ -43,14 +43,14 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/indexOf

若被查找的字符串 searchValue 是一个空字符串,将会产生“奇怪”的结果。如果 fromIndex 值为空,或者 fromIndex 值小于被查找的字符串的长度,返回值和以下的 fromIndex 值一样:

-
'hello world'.indexOf('') // 返回 0
+
'hello world'.indexOf('') // 返回 0
 'hello world'.indexOf('', 0) // 返回 0
 'hello world'.indexOf('', 3) // 返回 3
 'hello world'.indexOf('', 8) // 返回 8

另外,如果 fromIndex 值大于等于字符串的长度,将会直接返回字符串的长度(str.length):

-
'hello world'.indexOf('', 11) // 返回 11
+
'hello world'.indexOf('', 11) // 返回 11
 'hello world'.indexOf('', 13) // 返回 11
 'hello world'.indexOf('', 22) // 返回 11
@@ -60,7 +60,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/indexOf

字符串中的字符被从左向右索引。第一个字符的索引(index)是 0,变量名为 stringName 的字符串的最后一个字符的索引是 stringName.length - 1 。

-
"Blue Whale".indexOf("Blue")       // 返回 0
+
"Blue Whale".indexOf("Blue")       // 返回 0
 "Blue Whale".indexOf("Blute")      // 返回 -1
 "Blue Whale".indexOf("Whale", 0)   // 返回 5
 "Blue Whale".indexOf("Whale", 5)   // 返回 5
@@ -71,14 +71,14 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/indexOf
 
 

indexOf 方法是区分大小写的。例如,下面的表达式将返回 -1

-
"Blue Whale".indexOf("blue")      // 返回 -1
+
"Blue Whale".indexOf("blue")      // 返回 -1
 

检测是否存在某字符串

注意 0 并不会被当成 true-1 不会被当成 false 。所以当检测某个字符串是否存在于另一个字符串中时,可使用下面的方法:

-
'Blue Whale'.indexOf('Blue') !== -1    // true
+
'Blue Whale'.indexOf('Blue') !== -1    // true
 'Blue Whale'.indexOf('Bloe') !== -1    // false
 ~('Blue Whale'.indexOf('Bloe'))        // 0, 这是一种错误用法
@@ -88,7 +88,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/indexOf

下例使用 indexOf()lastIndexOf() 方法定位字符串中 "Brave new world" 的值。

-
var anyString = "Brave new world";
+
var anyString = "Brave new world";
 
 console.log("The index of the first w from the beginning is " + anyString.indexOf("w"));
 // logs 8
@@ -105,7 +105,7 @@ console.log("The index of 'new' from the end is " + anyString.lastIndexOf("new")
 
 

下例定义了两个字符串变量。两个变量包含相同的字符串,除了第二个字符串中的某些字符为大写。第一个 log 方法输出 19。但是由于 indexOf 方法区分大小写,因此不会在 myCapString 中发现字符串 “cheddar",所以,第二个 log 方法会输出 -1。

-
var myString    = "brie, pepper jack, cheddar";
+
var myString    = "brie, pepper jack, cheddar";
 var myCapString = "Brie, Pepper Jack, Cheddar";
 
 console.log('myString.indexOf("cheddar") is ' + myString.indexOf("cheddar"));
@@ -117,7 +117,7 @@ console.log('myCapString.indexOf("cheddar") is ' + myCapString.indexOf("cheddar"
 
 

在下例中,设置了 count 来记录字母 e 在字符串 str 中出现的次数:

-
// 翻译:生存还是毁灭?这是个问题。(莎士比亚《哈姆雷特》)
+
// 翻译:生存还是毁灭?这是个问题。(莎士比亚《哈姆雷特》)
 var str = 'To be, or not to be, that is the question.';
 var count = 0;
 var pos = str.indexOf('e');
-- 
cgit v1.2.3-54-g00ecf