[JavaScript]-----String類型

四.String類型

String 類型包含了三個屬性和大量的可用內置方法。

String對象屬性

屬性 描述
length
返回字符串的字符長度
constructor
返回創建String 對象的函數
prototype
通過添加屬性和方法擴展字符串定義
字符方法

方法 描述
charAt(n) 返回指定索引位置的字符
charCodeAt(n)
以Unicode 編碼形式返回指定索引位置的字符

var box = 'Mr.Lee';
alert(box.charAt(1)); //r
alert(box.charCodeAt(1)); //114
alert(box[1]); //r,通過數組方式截取

字符串操作方法

方法 描述
concat(str1,str2,..) 將字符串參數串聯到調用該方法的字符串
slice(n,m)
返回字符串n 到m 之間位置的字符串
substring(n,m)
同上
substr(n,m)
返回字符串n 開始的m 個字符串

var box = 'Mr.Lee';
alert(box.concat(' is ', ' Teacher ', '!')); //Mr.Lee is Teacher !
alert(box.slice(3)); //Lee
alert(box.slice(3,5)); //Le
alert(box.substring(3)); //Lee
alert(box.substring(3,5)); //Le
alert(box.substr(3)); //Lee
alert(box.substr(3,5)); //Lee


大小寫轉換方法

方法 描述
toLowerCase(str) 將字符串全部轉換爲小寫
toUpperCase(str) 將字符串全部轉換爲大寫
toLocaleLowerCase(str) 將字符串全部轉換爲小寫,並且本地化
toLocaleupperCase(str) 將字符串全部轉換爲大寫,並且本地化
var box = 'Mr.Lee is Lee';
alert(box.toLowerCase()); //全部小寫
alert(box.toUpperCase()); //全部大寫
alert(box.toLocaleLowerCase()); //
alert(box.toLocaleUpperCase()); //


字符串的模式匹配方法

方法 描述
split(pattern) 返回字符串按指定pattern 拆分的數組
match(pattern) 返回pattern 中的子串或null
replace(pattern, replacement) 用replacement 替返回字符串中pattern 開始位置換pattern
search(pattern) 返回字符串中pattern 開始位置

其它方法

fromCharCode(ascii)
靜態方法,輸出Ascii 碼對應值
localeCompare(str1,str2)
比較兩個字符串,並返回相應的值
   


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章