標籤:ima cto sea from min 開始 str soft 沒有
1.字串的建立
var str = "Hello Microsoft!";
2.字串屬性
constructor 返回建立字串屬性的函數 length 返回字串的長度 prototype 允許您向對象添加屬性和方法
3.字串方法
- (1)charAt() —-返回字串在下標index位置的字元
- stringObject.charAt(index) // 如果參數index不在0與字串長度之間,則返回一個空的字串
- ‘kdfjrjrtj‘.charAt(3); // ====>返回 ‘j‘;
- (2) charCodeAt() —- 返回字串相應下標index位置的字元的unicode編碼
- stringObject.charCodeAt(index) // 返回unicode編碼
- ‘sjdlfkjsrf‘.charCodeAt(3); // 108 (‘l‘的unicode編碼為108)
- (3) indexOf() —-返回某個指定的字串值在字串中首次出現的位置
- stringObject.indexOf(searchvalue,fromindex); // searchvalue為需要搜尋的子字串,fromindex為從某個下標開始查詢
- ‘hello world!‘.indexOf(‘lo wo‘); // 3 (子字串首次出現的位置)
- (4) match() —- 在字串內檢索指定的值,或找到一個或多個Regex的匹配
- stringObject.match(regexp) // regexp為匹配子字串的Regex
- var str = ‘hello world , l love the world‘;
- str.match(/world/g); // [world, world]
- (5) replace()--需要替代的字串
- var str = ‘hello world , l love the world‘;
- str.replace(/world/g,‘java‘); // "hello java , l love the java"
- // 其中str並沒有被改變
學習筆記:javascript內建對象:字串對象