javascript
[记录] JavaScript 中的字符串操作
字符串原型:
通過修改字符串的原型,可以為所有字符串添加公共方法
JS 中的字符串操作
字符串:基本數(shù)據(jù)類型,一旦定義就不會被修改,如果修改則是重新開辟空間存儲。字符串有屬性length和一系列方法。
字符串的生成轉換 (可以將任何類型的數(shù)據(jù)轉換為字符串)
轉換成字符串的三種方式:
根據(jù)索引查找字符:
1. str.charAt(索引值); // 獲取指定索引上的字符
2. str[索引值] : str[0] 和數(shù)組一樣,通過下標獲取,H5新增,IE6-7-8不支持
var str = 'ABC123你好!'; console.log( str[1] ); // B console.log( str[str.length-1] ); // ! console.log( str[10000] ); // 索引超出邊界返回undefined3. str.charCodeAt(索引值); // 獲取指定索引上的Unicode編碼
var str = 'ABC123你好!'; console.log( str.charCodeAt(1) ); // 66 console.log( str.charCodeAt(str.length-1) ); // 65281 console.log( str.charCodeAt(1000) ); // 索引超出邊界返回NaN根據(jù)字符查索引:
1. str.indexOf('字符串'); 從左往右查, 返回當前存在字符的位置
2. str.lastIndexOf('字符串'); 從右往左查,返回當前存在字符的位置
var str = 'ABC123你好!'; console.log( str.lastIndexOf("C") ); // 2 console.log( str.lastIndexOf("Ga") ); // 未找到匹配字符,返回-1字符串拼接:
1. 使用 +號 拼接字符串
2. concat() 方法可以拼接字符串,也能拼接數(shù)組
// 拼接字符串 str = str.concat('深圳'); // ABC123你好!深圳// 拼接數(shù)組 [1,5,9].concat([6,2,8]); // [1,5,9,6,2,8]字符串截取:
1. arrayObject.slice(開始索引值,結束索引值); // 由于字符串是個類數(shù)組,所以slice能截取數(shù)組和字符串; 不改變原數(shù)據(jù),返回值是剪切的內容
2. str.substr(開始索引值,截取個數(shù));
var str = 'ABC123你好!'; // 沒有參數(shù)時: 拷貝一份 console.log( str.substr() ); // 'ABC123你好!' // 一個參數(shù)時: 從開始索引值截取到最后 console.log( str.substr(3) ); // '123你好!' // 兩個參數(shù)時: 從開始索引值截取1個 console.log( str.substr(2,1) ); // 'C' // 當參數(shù)為負數(shù)時: 從右往左數(shù) console.log( str.substr(-2) ); // '好!' 總結: a). 不改變原數(shù)據(jù),返回值為截取的內容。 b). 支持負數(shù),一個參數(shù)時,從開始索引值截取到最后 c). 兩個參數(shù)時, 從開始位置, 截取指定長度的字符3. str.substring(開始索引值,結束索引值);
var str = 'ABC123你好!'; // 沒有參數(shù)時: 拷貝一份 console.log( str.substring() ); // 'ABC123你好!' // 一個參數(shù)時: 從開始索引值截取到最后 console.log( str.substring(3) ); // '123你好!' // 兩個參數(shù)時: 包左不包右 console.log( str.substring(0,3) ); // 'ABC' // 不支持負數(shù),如果是負數(shù)則視為0 console.log( str.substring( -11, 3) ); // 'ABC' // 前大后小,則智能調換位置 console.log( str.substring(6,3) ); // '123' 總結: a). 不改變原數(shù)據(jù),返回值為截取的內容。 b). 不支持負數(shù),如果是負數(shù)則視為0 c). 兩個參數(shù)時,包左不包右,如果前大后小,則智能調換位置。字符串替換:
1. str.replace(regExp/substr, 替換的內容);
字符串大小寫轉換:
var str = 'ABC123你好!'; str.toLowerCase(); // 英文字符轉換成小寫 var str = 'abc123你好!'; str.toUpperCase(); // 英文字符轉化成大寫字符串分割: split() 和 join() 是一對
str.split(分隔符[,分割長度]); // 用于把一個字符串分割成字符串數(shù)組 var str = 'aaa|bbb|ccc'; // 按'|'進行分割,參數(shù)不會出現(xiàn)在數(shù)組中; console.log( str.split('|') ); // ["aaa","bbb","ccc"] // 不帶參數(shù),整體作為一個元素 console.log( str.split() ); // ["aaa|bbb|ccc"] // 按空字符串分割,則每個字符都為一個元素 console.log( str.split("") ); // ["a","a","a","|","b","b","b","|","c","c","c"] // 指定長度分割 console.log( str.split("", 2) ); // ["a","a"]字符串匹配:
1. str.search(regexp); // 匹配指定字符串, 返回起始位置,匹配不成功則返回 -1; 不執(zhí)行全局匹配,匹配成功則不再匹配
2. str.match(substr/regexp); // 返回匹配結果的數(shù)組,匹配失敗返回null
var str = 'A123B345ABC456"; // 在字符串內檢索指定的值,或找到一個或多個匹配 console.log( str.match(/A/ig); // ["A","A"] // 未找到匹配內容返回 null console.log( str.match(/DDD/ig); // null3. 正則方法: reg.test(str); // 返回 true 或 false
var str = 'ABC123你好!'; var reg = /ABC/ig; console.log( reg.test(str) ); // true; var reg = /DDD/ig; cosnole.log( reg.test(str) ); // false;4. 正則方法: reg.exec(str); 返回一個數(shù)組,存放匹配結果,未找到,返回null
字符串案例:
1. 統(tǒng)計一個字符串中出現(xiàn)次數(shù)最多的字符和次數(shù)。
例如: "Hollow word! good day!" => o, 5次
2. 獲取URL中?后面的內容,并轉化成對象的形式;
例如: "https://www.baidu.com/login?name=yuxi2018&password=123456&type=1" => { name: "yuxi2018", password: "123456", type: 1 }
3. 字符串去重
例如: "aabbcc123" => "abc123";
4. 生成駝峰法字符串
例如: border-bottom-color => borderBottomColor
轉載于:https://www.cnblogs.com/yuxi2018/p/9531311.html
總結
以上是生活随笔為你收集整理的[记录] JavaScript 中的字符串操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: EasyUI 分页 偶遇 问题
- 下一篇: Open images from USB