[TOC]
>[info]String 對象用于處理文本(字符串)。下面列舉一些范例。
## length 屬性
>[info]length 屬性可返回字符串中的字符數目。
~~~
<script type="text/javascript">
var txt="Hello World!"
document.write(txt.length)
</script>
~~~
## charAt() 方法
>[info]charAt() 方法可返回指定位置的字符。
在字符串 "Hello world!" 中,我們將返回位置 1 的字符:
~~~
<script type="text/javascript">
var str="Hello world!"
document.write(str.charAt(1))
</script>
~~~
>[danger]參數:表示字符串中某個位置的數字,即字符在字符串中的下標。
## slice() 方法
>[info]slice() 方法可提取字符串的某個部分,并以新的字符串返回被提取的部分。
~~~
<script type="text/javascript">
stringObject.slice(start,end);
</script>
~~~
<table class="dataintable">
<tbody>
<tr>
<th>
參數
</th>
<th>
描述
</th>
</tr>
<tr>
<td>
start
</td>
<td>
要抽取的片斷的起始下標。如果是負數,則該參數規定的是從字符串的尾部開始算起的位置。也就是說,-1 指字符串的最后一個字符,-2 指倒數第二個字符,以此類推。
</td>
</tr>
<tr>
<td>
end
</td>
<td>
緊接著要抽取的片段的結尾的下標。若未指定此參數,則要提取的子串包括 start 到原字符串結尾的字符串。如果該參數是負數,那么它規定的是從字符串的尾部開始算起的位置。
</td>
</tr>
</tbody>
在本例中,我們將提取從位置 6 到位置 11 的所有字符:
~~~
<script type="text/javascript">
var str="Hello happy world!"
document.write(str.slice(6,11))
</script>
~~~
## substr() 方法
>[info]substr() 方法可在字符串中抽取從 start 下標開始的指定數目的字符。
~~~
<script type="text/javascript">
stringObject.substr(start,length)
</script>
~~~
<table class="dataintable">
<tbody>
<tr>
<th>
參數
</th>
<th>
描述
</th>
</tr>
<tr>
<td>
<i>
start
</i>
</td>
<td>
必需。要抽取的子串的起始下標。必須是數值。如果是負數,那么該參數聲明從字符串的尾部開始算起的位置。也就是說,-1 指字符串中最后一個字符,-2
指倒數第二個字符,以此類推。
</td>
</tr>
<tr>
<td>
<i>
length
</i>
</td>
<td>
可選。子串中的字符數。必須是數值。如果省略了該參數,那么返回從
<i>
stringObject
</i>
的開始位置到結尾的字串。
</td>
</tr>
</tbody>
</table>
在本例中,我們將使用 substr() 從字符串中提取一些字符:
~~~
<script type="text/javascript">
var str="Hello world!"
document.write(str.substr(3,7))
</script>
~~~
## indexOf() 方法
>[info]indexOf() 方法可返回某個指定的字符串值在字符串中首次出現的位置。
~~~
<script type="text/javascript">
stringObject.indexOf(searchvalue,fromindex)
</script>
~~~
<table class="dataintable">
<tbody>
<tr>
<th>
參數
</th>
<th>
描述
</th>
</tr>
<tr>
<td>
searchvalue
</td>
<td>
必需。規定需檢索的字符串值。
</td>
</tr>
<tr>
<td>
fromindex
</td>
<td>
可選的整數參數。規定在字符串中開始檢索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略該參數,則將從字符串的首字符開始檢索。
</td>
</tr>
</tbody>
</table>
在本例中,我們將在 "Hello world!" 字符串內進行不同的檢索:
~~~
<script type="text/javascript">
var str="Hello world!"
document.write(str.indexOf("world") + "<br />")
</script>
~~~
## match() 方法
>[info]match() 方法可在字符串內檢索指定的值,或找到一個或多個正則表達式的匹配。
~~~
<script type="text/javascript">
stringObject.match(searchvalue)
stringObject.match(regexp)
</script>
~~~
<table class="dataintable">
<tbody>
<tr>
<th>
參數
</th>
<th>
描述
</th>
</tr>
<tr>
<td>
searchvalue
</td>
<td>
必需。規定要檢索的字符串值。
</td>
</tr>
<tr>
<td>
regexp
</td>
<td>
必需。規定要匹配的模式的 RegExp 對象。如果該參數不是 RegExp 對象,則需要首先把它傳遞給 RegExp 構造函數,將其轉換為
RegExp 對象。
</td>
</tr>
</tbody>
</table>
在本例中,我們將在 "Hello world!" 中進行不同的檢索:
~~~
<script type="text/javascript">
var str="Hello world!"
document.write(str.match("world") + "<br />")
document.write(str.match("World") + "<br />")
document.write(str.match("worlld") + "<br />")
document.write(str.match("world!"))
</script>
~~~
## replace()方法
>[info]replace() 方法用于在字符串中用一些字符替換另一些字符,或替換一個與正則表達式匹配的子串。
~~~
<script type="text/javascript">
stringObject.replace(regexp/substr,replacement)
</script>
~~~
<table class="dataintable">
<tbody>
<tr>
<th>
參數
</th>
<th>
描述
</th>
</tr>
<tr>
<td>
regexp/substr
</td>
<td>
<p>
必需。規定子字符串或要替換的模式的 RegExp 對象。
</p>
<p>
請注意,如果該值是一個字符串,則將它作為要檢索的直接量文本模式,而不是首先被轉換為 RegExp 對象。
</p>
</td>
</tr>
<tr>
<td>
replacement
</td>
<td>
必需。一個字符串值。規定了替換文本或生成替換文本的函數。
</td>
</tr>
</tbody>
</table>
在本例中,我們將使用 "xuebingsi" 替換字符串中的 "Microsoft":
~~~
<script type="text/javascript">
var str="Visit Microsoft!"
document.write(str.replace(/Microsoft/, "xuebingsi"))
</script>
~~~
## split()方法
>[info]split() 方法用于把一個字符串分割成字符串數組。
~~~
<script type="text/javascript">
stringObject.split(separator,howmany)
</script>
~~~
<table class="dataintable">
<tbody>
<tr>
<th>
參數
</th>
<th>
描述
</th>
</tr>
<tr>
<td>
separator
</td>
<td>
必需。字符串或正則表達式,從該參數指定的地方分割 stringObject。
</td>
</tr>
<tr>
<td>
howmany
</td>
<td>
可選。該參數可指定返回的數組的最大長度。如果設置了該參數,返回的子串不會多于這個參數指定的數組。如果沒有設置該參數,整個字符串都會被分割,不考慮它的長度。
</td>
</tr>
</tbody>
</table>
在本例中,我們將按照不同的方式來分割字符串:
~~~
<script type="text/javascript">
var str="How are you doing today?"
document.write(str.split(" ") + "<br />")
document.write(str.split("") + "<br />")
document.write(str.split(" ",3))
</script>
~~~
## toLowerCase()方法
>[info]toLowerCase() 方法用于把字符串轉換為小寫。
~~~
<script type="text/javascript">
stringObject.toLowerCase()
</script>
~~~
## toUpperCase()方法
>[info]toUpperCase() 方法用于把字符串轉換為大寫。
~~~
<script type="text/javascript">
stringObject.toUpperCase()
</script>
~~~
>[danger]**作業:**把字符串 “NidfdegdHdrE” 寫一個函數轉成 “nIDFDEGDhDRe”。
<table class="dataintable">
<tbody>
<tr>
<th style="width:30%">
方法
</th>
<th>
描述
</th>
</tr>
<tr>
<td>
<a>
anchor()
</a>
</td>
<td>
創建 HTML 錨。
</td>
</tr>
<tr>
<td>
<a>
big()
</a>
</td>
<td>
用大號字體顯示字符串。
</td>
</tr>
<tr>
<td>
<a>
blink()
</a>
</td>
<td>
顯示閃動字符串。
</td>
</tr>
<tr>
<td>
<a>
bold()
</a>
</td>
<td>
使用粗體顯示字符串。
</td>
</tr>
<tr>
<td>
<a>
charAt()
</a>
</td>
<td>
返回在指定位置的字符。
</td>
</tr>
<tr>
<td>
<a>
charCodeAt()
</a>
</td>
<td>
返回在指定的位置的字符的 Unicode 編碼。
</td>
</tr>
<tr>
<td>
<a>
concat()
</a>
</td>
<td>
連接字符串。
</td>
</tr>
<tr>
<td>
<a>
fixed()
</a>
</td>
<td>
以打字機文本顯示字符串。
</td>
</tr>
<tr>
<td>
<a>
fontcolor()
</a>
</td>
<td>
使用指定的顏色來顯示字符串。
</td>
</tr>
<tr>
<td>
<a>
fontsize()
</a>
</td>
<td>
使用指定的尺寸來顯示字符串。
</td>
</tr>
<tr>
<td>
<a>
fromCharCode()
</a>
</td>
<td>
從字符編碼創建一個字符串。
</td>
</tr>
<tr>
<td>
<a>
indexOf()
</a>
</td>
<td>
檢索字符串。
</td>
</tr>
<tr>
<td>
<a>
italics()
</a>
</td>
<td>
使用斜體顯示字符串。
</td>
</tr>
<tr>
<td>
<a>
lastIndexOf()
</a>
</td>
<td>
從后向前搜索字符串。
</td>
</tr>
<tr>
<td>
<a>
link()
</a>
</td>
<td>
將字符串顯示為鏈接。
</td>
</tr>
<tr>
<td>
<a>
localeCompare()
</a>
</td>
<td>
用本地特定的順序來比較兩個字符串。
</td>
</tr>
<tr>
<td>
<a>
match()
</a>
</td>
<td>
找到一個或多個正則表達式的匹配。
</td>
</tr>
<tr>
<td>
<a>
replace()
</a>
</td>
<td>
替換與正則表達式匹配的子串。
</td>
</tr>
<tr>
<td>
<a>
search()
</a>
</td>
<td>
檢索與正則表達式相匹配的值。
</td>
</tr>
<tr>
<td>
<a>
slice()
</a>
</td>
<td>
提取字符串的片斷,并在新的字符串中返回被提取的部分。
</td>
</tr>
<tr>
<td>
<a>
small()
</a>
</td>
<td>
使用小字號來顯示字符串。
</td>
</tr>
<tr>
<td>
<a>
split()
</a>
</td>
<td>
把字符串分割為字符串數組。
</td>
</tr>
<tr>
<td>
<a>
strike()
</a>
</td>
<td>
使用刪除線來顯示字符串。
</td>
</tr>
<tr>
<td>
<a>
sub()
</a>
</td>
<td>
把字符串顯示為下標。
</td>
</tr>
<tr>
<td>
<a>
substr()
</a>
</td>
<td>
從起始索引號提取字符串中指定數目的字符。
</td>
</tr>
<tr>
<td>
<a>
substring()
</a>
</td>
<td>
提取字符串中兩個指定的索引號之間的字符。
</td>
</tr>
<tr>
<td>
<a>
sup()
</a>
</td>
<td>
把字符串顯示為上標。
</td>
</tr>
<tr>
<td>
<a>
toLocaleLowerCase()
</a>
</td>
<td>
把字符串轉換為小寫。
</td>
</tr>
<tr>
<td>
<a>
toLocaleUpperCase()
</a>
</td>
<td>
把字符串轉換為大寫。
</td>
</tr>
<tr>
<td>
<a>
toLowerCase()
</a>
</td>
<td>
把字符串轉換為小寫。
</td>
</tr>
<tr>
<td>
<a>
toUpperCase()
</a>
</td>
<td>
把字符串轉換為大寫。
</td>
</tr>
<tr>
<td>
toSource()
</td>
<td>
代表對象的源代碼。
</td>
</tr>
<tr>
<td>
<a>
toString()
</a>
</td>
<td>
返回字符串。
</td>
</tr>
<tr>
<td>
<a>
valueOf()
</a>
</td>
<td>
返回某個字符串對象的原始值。
</td>
</tr>
</tbody>
</table>
- 序言
- 第一章:準備工作
- 寫在學習之前的話
- web應用開發結構
- 開發工具/環境
- 第二章:展現層面(HTML/CSS)
- HTML簡介
- HTML基礎
- HTML編碼
- HTML鏈接
- HTML圖像
- HTML列表
- HTML表單
- HTML表格(分水嶺)
- HTML 實體
- HTML框架
- CSS層疊樣式表
- CSS選擇器
- CSS文本/字體
- CSS繼承和疊加
- CSS框模型
- CSS浮動(分水嶺)
- CSS定位
- CSS背景
- CSS圖標字體
- CSS補充
- 開發技巧
- 第三章:展現層面(Javascript)
- JS簡介
- JS實現
- JS輸出交互
- JS變量
- JS數據類型
- JS運算符
- JS流程控制(分水嶺)
- JS函數
- JS數組
- JS對象(分水嶺)
- JS數組對象
- JS字符串對象
- JS數學對象
- JS日期對象
- JS BOM對象(分水嶺)
- JS DOM對象
- JS事件對象
- JS元素對象
- JS DOM節點
- 第四章:展現層面(Jquery)
- JQ簡介
- JQ使用
- JQ選擇器
- JQ篩選
- JQ屬性
- JQ-CSS
- JQ事件
- JQ文檔處理
- JQ效果
- JQ-ajax
- 第五章:邏輯/業務層面(PHP)
- PHP簡介
- PHP變量
- PHP數據類型
- PHP常量
- PHP運算符
- PHP流程控制
- PHP函數(分水嶺)
- PHP日期
- PHP數學
- PHP數組
- PHP字符串
- PHP正則表達式(分水嶺)
- PHP目錄操作
- PHP文件
- PHP上傳/下載
- PHP面向對象(分水嶺)
- PHP圖像處理
- PHP會話控制
- Ajax異步處理
- PHPMysql擴展
- PHPMysqli擴展
- PHPPdo擴展
- PHP接口
- PHP命名空間
- 第六章:邏輯/業務層面(框架設計)
- 第七章:存儲層面(mysql)
- Mysql基礎
- Mysql Sql簡介
- Mysql數據庫
- Mysql數據類型
- Mysql數據表
- Mysql操作記錄
- Mysql查詢
- Mysql修改表結構
- Mysql日期與時間
- Mysql分組統計
- Mysql多表查詢
- Mysql安全
- Mysql存儲引擎
- Mysql事務
- Mysql視圖
- Mysql觸發器
- Mysql存儲過程
- Mysql存儲函數
- Mysql優化
- 第八章:服務器(Linux)
- Linux介紹與安裝
- Shell
- 目錄與文件操作
- VIM編輯器使用
- 帳號管理
- SUDO
- 權限控制
- 壓縮與打包
- 軟件安裝
- 計劃任務
- 進程管理
- 寶塔Linux面板