?? day2_3.html
字號:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312-80">
<style type="text/css">
<!--
a:link { color: blue; text-decoration: none}
a:visited { color: purple; text-decoration: none}
a:hover { color: #CC0033; text-decoration: underline}
-->
</style>
<title>JavaScript高級教程</title>
</head>
<body topmargin="1" leftmargin="2">
<table border="0" width="591" cellspacing="0">
<tr>
<td bgcolor="#ffff99" width="451">JavaScript高級教程 - 第二課</td>
</tr>
<tr>
<td bgcolor="#FF6600" width="451"><a href="mailto:thau@wired.com">Thau</a></td>
</tr>
</table>
<div align="left">
<table border="0" width="630" cellspacing="0">
<tr>
<td width="458" valign="top" align="left" rowspan="2"><small><small><br>
</small></small><strong>第三頁:子字符串</strong>
<p>子字符串(substring)和charAt有些象,不同之處在于它能夠<br>
從一個單詞中抓取整個的子字符串,而不只是字母,這里是其<br>
格式:</p>
<p>var the_substring = the_string.substring(from, to);</p>
<p>"From"指的是子字符串中第1個字母的位置,"to"有點奇特,<br>
它是該子字符串中比最后一個位置大1的位置.使用這種神奇<br>
的方法你可以標記子字符串的起始和結束位置,用"to"的位置<br>
減去"from"的位置就會得出該子字符串的長度:</p>
<p>var the_string = "monkey";<br>
<br>
var clergy = the_string.substring(0,4);<br>
<br>
var tool = the_string.substring(3,6);</p>
<p>運行該段代碼后變量clergy的值為"monk"; 變量tool的值為<br>
"key"。</p>
<p>子字符串常和indexOf一起使用,將字符串分成若干塊.例如,<br>
你可以從一個給定的URL中抽取出其域名:</p>
<p>var the_url = prompt("What's the URL?","");<br>
<br>
var lead_slashes = the_url.indexOf("//");<br>
<br>
var domain_start = lead_slashes + 2;<br>
<br>
var without_resource = the_url.substring(domain_start, the_url.length);<br>
<br>
var next_slash = without_resource.indexOf("/");<br>
<br>
var domain = without_resource.substring(0, next_slash);</p>
<p>這段代碼的意思是:如果你輸入<br>
<a href="http://www.webmonkey.com/javascript/index.html">"http://www.webmonkey.com/javascript/index.html"</a>,則域<br>
名就是"www.webmonkey.com" .如果這個方法對你來說有些麻<br>
煩,我將向你介紹如何使用split方法簡化其執行過程.但是首<br>
先我們作一些分析.</p>
<p>基本的技巧是將第1個斜杠和第2個斜杠之間的內容分離出來:<br>
var the_url = prompt("What's the URL?",""); <br>
這行代碼向用戶詢問一個URL.假設用戶輸入了<br>
"http://www.webmonkey.com/javascript/index.html." <br>
var lead_slashes = the_url.indexOf("//"); <br>
這行代碼確定第一個雙斜杠的位置.在本例中lead_slashes<br>
的值是5,因為雙斜杠的位置從5開始.</p>
<p>你可能會想,通常德URL都是以http://開始,所以雙斜杠的位<br>
置肯定是在5開始,為什么還要加入indexOf這一段多余的代<br>
碼呢?但是問題的關鍵在于你不知道用戶在填入URL時是否一定<br>
填入http:,他們也許會不小心多鍵入了一個空格,也許他們所<br>
鍵入的URL在一個加密服務器上,其URL是<br>
"https://www.whatever.com/" .在編程你必須預料到種種可能<br>
發生的問題.所以我們必須用indexOf方法確定雙斜杠的確切的<br>
起始位置.</p>
<p>var domain_start = lead_slashes + 2; </p>
<p>這行代碼用于計算該域名的第1個字母的起始位置.由于這里<br>
有一個雙斜杠,所以域名第1個字母的起始位置應該在雙斜杠<br>
所在位置加2的位置.</p>
<p>var without_resource = the_url.substring(domain_start, the_string.length);
</p>
<p>這段代碼將域名起始位置往后的所有字符都提取出來.所以執<br>
行完這行代碼后without_resource是<br>
<a href="http://www.webmonkey.com/javascript/index.html.">"www.webmonkey.com/javascript/index.html."</a>
</p>
<p>var next_slash = without_resource.indexOf("/"); </p>
<p>這行代碼計算出該字符串中下一個斜杠的位置,而從該字符串<br>
起始位置到這個斜杠之間的內容就是域名.在本例中下一個斜<br>
杠的位置是17。</p>
<p>var domain = without_resource.substring(0, next_slash); </p>
<p>最后一步是提取出該字符串起始位置到下一個斜杠之間的所有<br>
內容.在本例中使得域名等同于<a href="http://www.webmonkey.com">"www.webmonkey.com"</a>。</p>
<p>這樣做確實很麻煩,利用split方法則可以使該過程容易很多.<a href="day2_4.html">>></a></p>
<p><font face="宋體" size="3" color="#000000"><strong>JavaScript高級教程</strong></font><font color="#FF0000" face="宋體" size="3"><br>
</font><font color="#FF0000">第一頁</font> <a href="day2_1.html">Javascript高級教程-第2日</a><br>
<font color="#FF0000">第二頁</font> <a href="day2_2.html">神奇的字符串處理</a><br>
<font color="#FF0000">第三頁</font> 子字符串<br>
<font color="#FF0000">第四頁</font> <a href="day2_4.html">分割方法(splitting
method)</a><br>
<font color="#FF0000">第五頁</font> <a href="day2_5.html">相關數組</a><br>
<font color="#FF0000">第六頁</font> <a href="day2_6.html">相關數組的一個例子</a><br>
<font color="#FF0000">第七頁</font> <a href="day2_7.html">介紹cookie</a><br>
<font color="#FF0000">第八頁</font> <a href="day2_8.html">深入了解cookies</a><br>
<font color="#FF0000">第九頁</font> <a href="day2_9.html">讀取cookies</a><br>
<font color="#FF0000">第十頁</font> <a href="day2_10.html">復雜的cookies讀取</a><br>
<font color="#FF0000">第十一頁</font> <a href="day2_11.html">讀取和編寫多重cookies</a><br>
<font color="#FF0000">第十二頁</font> <a href="day2_12.html">再次深入了解cookies</a><br>
<font color="#FF0000">第十三頁</font> <a href="day2_13.html">cookie路徑和域</a></p>
<p><font size="3">[<a href="day1_1.html">第1課</a>][第2課][<a href="day3_1.html">第3課</a>][<a href="day4_1.html">第4課</a>][<a href="day5_1.html">第5課</a>]</font></p>
<hr align="left">
<!--webbot bot="Include" U-Include="../../copyright.html" TAG="BODY" startspan -->
<p><font face="verdana, arial, geneva, sans-serif" size="2"><a href="http://phtshop.yeah.net" target="_top">本文根據
網猴 相關文章改編,版權歸原作者所有。</a> </font><font color="#000000"><span class="smallfont"></span></font></p>
<!--webbot bot="Include" endspan i-checksum="15926" --> </td>
</tr>
<tr> </tr>
</table>
</div>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -