mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-12-16 07:51:52 +08:00
语法高亮,滚动条美化,设置页面调整
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
<p><code><strong>indexOf()</strong></code> 方法返回调用 <a href="Reference/String" title="此页面仍未被本地化, 期待您的翻译!"><code>String</code></a> 对象中第一次出现的指定值的索引,开始在 fromIndex进行搜索。</p>
|
||||
<p>如果未找到该值,则返回-1。</p>
|
||||
<h2 id="Syntax" name="Syntax">语法</h2>
|
||||
<pre class="syntaxbox"><code><em>str</em>.indexOf(<em>searchValue</em>[, <em>fromIndex</em>]</code>)</pre>
|
||||
<pre><code class="language-javascript"><code><em>str</em>.indexOf(<em>searchValue</em>[, <em>fromIndex</em>]</code>)</code></pre>
|
||||
<h3 id="Parameters" name="Parameters">参数</h3>
|
||||
<dl>
|
||||
<dt><code>searchValue</code></dt>
|
||||
@@ -17,25 +17,25 @@
|
||||
</dl>
|
||||
<h2 id="Description" name="Description">描述</h2>
|
||||
<p>字符串中的字符被从左向右索引。首字符的索引(index)为 0,字符串 <code>stringName</code> 的最后一个字符的索引是 <code>stringName.length - 1</code>。</p>
|
||||
<pre class="brush: js">"Blue Whale".indexOf("Blue"); // returns 0
|
||||
<pre><code class="language-javascript">"Blue Whale".indexOf("Blue"); // returns 0
|
||||
"Blue Whale".indexOf("Blute"); // returns -1
|
||||
"Blue Whale".indexOf("Whale", 0); // returns 5
|
||||
"Blue Whale".indexOf("Whale", 5); // returns 5
|
||||
"Blue Whale".indexOf("", 9); // returns 9
|
||||
"Blue Whale".indexOf("", 10); // returns 10
|
||||
"Blue Whale".indexOf("", 11); // returns 10</pre>
|
||||
"Blue Whale".indexOf("", 11); // returns 10</code></pre>
|
||||
<h3 id="Example:_indexOf_and_case-sensitivity" name="Example:_indexOf_and_case-sensitivity">区分大小写</h3>
|
||||
<p><code>indexOf</code> 方法区分大小写。例如,下面的表达式返回 -1:</p>
|
||||
<pre class="brush: js">"Blue Whale".indexOf("blue") // returns -1
|
||||
</pre>
|
||||
<pre><code class="language-javascript">"Blue Whale".indexOf("blue") // returns -1
|
||||
</code></pre>
|
||||
<h3 id="检测是否存在某字符串">检测是否存在某字符串</h3>
|
||||
<p>当检测某个字符串是否存在于另一个字符串中时,可使用下面的方法:</p>
|
||||
<pre class="brush: js">"Blue Whale".indexOf("Blue") !== -1; // true
|
||||
"Blue Whale".indexOf("Bloe") !== -1; // false</pre>
|
||||
<pre><code class="language-javascript">"Blue Whale".indexOf("Blue") !== -1; // true
|
||||
"Blue Whale".indexOf("Bloe") !== -1; // false</code></pre>
|
||||
<h2 id="Examples" name="Examples">示例</h2>
|
||||
<h3 id="Example:_Using_indexOf_and_lastIndexOf" name="Example:_Using_indexOf_and_lastIndexOf">使用<code>indexOf()</code> 和 <code>lastIndexOf()</code></h3>
|
||||
<p>下例使用 <code>indexOf()</code> 和 <code>lastIndexOf()</code> 方法定位字符串中 "<code>Brave new world</code>" 的值。</p>
|
||||
<pre class="brush: js">var anyString = "Brave new world";
|
||||
<pre><code class="language-javascript">var anyString = "Brave new world";
|
||||
|
||||
console.log("The index of the first w from the beginning is " + anyString.indexOf("w"));
|
||||
// logs 8
|
||||
@@ -46,19 +46,19 @@ console.log("The index of 'new' from the beginning is " + anyString.indexOf("new
|
||||
// logs 6
|
||||
console.log("The index of 'new' from the end is " + anyString.lastIndexOf("new"));
|
||||
// logs 6
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="Example:_indexOf_and_case-sensitivity" name="Example:_indexOf_and_case-sensitivity"><code>indexOf</code> 和区分大小写</h3>
|
||||
<p>下例定义了两个字符串变量。两个变量包含相同的字符串,除了第二个字符串中的某些字符为大写。第一个 <code>log</code> 方法输出 19。但是由于 <code>indexOf</code> 方法区分大小写,因此不会在 <code>myCapString</code> 中发现字符串<code> “cheddar"</code>,结果第二个 <code>log</code> 方法输出 -1。</p>
|
||||
<pre class="brush: js">var myString = "brie, pepper jack, cheddar";
|
||||
<pre><code class="language-javascript">var myString = "brie, pepper jack, cheddar";
|
||||
var myCapString = "Brie, Pepper Jack, Cheddar";
|
||||
|
||||
console.log('myString.indexOf("cheddar") is ' + myString.indexOf("cheddar"));
|
||||
// logs 19
|
||||
console.log('myCapString.indexOf("cheddar") is ' + myCapString.indexOf("cheddar"));
|
||||
// logs -1</pre>
|
||||
// logs -1</code></pre>
|
||||
<h3 id="Example:_Using_indexOf_to_count_occurrences_of_a_letter_in_a_string" name="Example:_Using_indexOf_to_count_occurrences_of_a_letter_in_a_string">使用 <code>indexOf</code> 统计一个字符串中某个字母出现的次数</h3>
|
||||
<p>在下例中,设置了 <code>count</code> 来记录字母 <font face="Consolas, Liberation Mono, Courier, monospace">e</font> 在字符串 <code>str</code> 中出现的次数:</p>
|
||||
<pre class="brush: js">var str = 'To be, or not to be, that is the question.';
|
||||
<pre><code class="language-javascript">var str = 'To be, or not to be, that is the question.';
|
||||
var count = 0;
|
||||
var pos = str.indexOf('e');
|
||||
|
||||
@@ -68,7 +68,7 @@ while (pos !== -1) {
|
||||
}
|
||||
|
||||
console.log(count); // displays 4
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h2 id="规范">规范</h2>
|
||||
<table class="standard-table">
|
||||
<tbody>
|
||||
|
||||
Reference in New Issue
Block a user