日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

[记录] JavaScript 中的字符串操作

發(fā)布時(shí)間:2024/4/15 javascript 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [记录] JavaScript 中的字符串操作 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

字符串原型:
通過修改字符串的原型,可以為所有字符串添加公共方法

String.prototype.startwith = function(text) {return this.indexOf(text) == 0; }; var str = 'ABC123你好!'; str.startwith('A'); // 調(diào)用原型方法


JS 中的字符串操作
字符串:基本數(shù)據(jù)類型,一旦定義就不會(huì)被修改,如果修改則是重新開辟空間存儲(chǔ)。字符串有屬性length和一系列方法。
字符串的生成轉(zhuǎn)換 (可以將任何類型的數(shù)據(jù)轉(zhuǎn)換為字符串)
轉(zhuǎn)換成字符串的三種方式:

.toString() 方法 注: undefined、null沒有toString()方法。 var num = 28; console.log(typeof num.toString()); // 返回結(jié)果是"string", String() 方法 注: undefiend、null可以通過String()轉(zhuǎn)換為字符串(A + "") 拼接字符串 注: 所有的類型都可以通過拼接字符串的形式轉(zhuǎn)換成字符串

根據(jù)索引查找字符:
1. str.charAt(索引值); // 獲取指定索引上的字符

var str = 'ABC123你好!'; console.log( str.charAt(1) ); // B console.log( str.charAt(str.length-1) ); // ! console.log( str.charAt(1000) ); // '' 索引超出邊界返回空

2. str[索引值] : str[0] 和數(shù)組一樣,通過下標(biāo)獲取,H5新增,IE6-7-8不支持

var str = 'ABC123你好!'; console.log( str[1] ); // B console.log( str[str.length-1] ); // ! console.log( str[10000] ); // 索引超出邊界返回undefined

3. 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('字符串'); 從左往右查, 返回當(dāng)前存在字符的位置

var str = 'ABC123你好!'; console.log( str.indexOf("C") ); // 2 console.log( str.indexOf("Ga") ); // 未找到匹配字符,返回 -1

2. str.lastIndexOf('字符串'); 從右往左查,返回當(dāng)前存在字符的位置

var str = 'ABC123你好!'; console.log( str.lastIndexOf("C") ); // 2 console.log( str.lastIndexOf("Ga") ); // 未找到匹配字符,返回-1

字符串拼接:
1. 使用 +號(hào) 拼接字符串

var str = 'ABC123你好!'; str = str + '深圳'; // ABC123你好!深圳

2. concat() 方法可以拼接字符串,也能拼接數(shù)組

// 拼接字符串 str = str.concat('深圳'); // ABC123你好!深圳// 拼接數(shù)組 [1,5,9].concat([6,2,8]); // [1,5,9,6,2,8]

字符串截取:
1. arrayObject.slice(開始索引值,結(jié)束索引值); // 由于字符串是個(gè)類數(shù)組,所以slice能截取數(shù)組和字符串; 不改變?cè)瓟?shù)據(jù),返回值是剪切的內(nèi)容

var str = 'ABC123你好!'; // 沒有參數(shù)時(shí): 拷貝一份 console.log( str.slice() ); // 'ABC123你好!' // 一個(gè)參數(shù)時(shí): 從開始索引值截取到最后 console.log( str.slice(3) ); // '123你好!' // 兩個(gè)參數(shù)時(shí): 包左不包右 console.log( str.slice(0,3) ); // 'ABC' // 當(dāng)參數(shù)為負(fù)數(shù)時(shí): 從右往左數(shù) console.log( str.slice(-2) ); // '好!' // 前大后小 則返回空字符串 console.log( str.slice(3,0) ); // 返回 空字符串 總結(jié): a). 支持對(duì)數(shù)組和字符串的截取(不改變?cè)瓟?shù)據(jù),返回值為截取內(nèi)容) b). 支持負(fù)數(shù),一個(gè)參數(shù)時(shí),從開始索引值截取到最后 c). 兩個(gè)參數(shù)時(shí),包左不包右,如果前大后小,則返回空字符串或數(shù)組

2. str.substr(開始索引值,截取個(gè)數(shù));

var str = 'ABC123你好!'; // 沒有參數(shù)時(shí): 拷貝一份 console.log( str.substr() ); // 'ABC123你好!' // 一個(gè)參數(shù)時(shí): 從開始索引值截取到最后 console.log( str.substr(3) ); // '123你好!' // 兩個(gè)參數(shù)時(shí): 從開始索引值截取1個(gè) console.log( str.substr(2,1) ); // 'C' // 當(dāng)參數(shù)為負(fù)數(shù)時(shí): 從右往左數(shù) console.log( str.substr(-2) ); // '好!' 總結(jié): a). 不改變?cè)瓟?shù)據(jù),返回值為截取的內(nèi)容。 b). 支持負(fù)數(shù),一個(gè)參數(shù)時(shí),從開始索引值截取到最后 c). 兩個(gè)參數(shù)時(shí), 從開始位置, 截取指定長度的字符

3. str.substring(開始索引值,結(jié)束索引值);

var str = 'ABC123你好!'; // 沒有參數(shù)時(shí): 拷貝一份 console.log( str.substring() ); // 'ABC123你好!' // 一個(gè)參數(shù)時(shí): 從開始索引值截取到最后 console.log( str.substring(3) ); // '123你好!' // 兩個(gè)參數(shù)時(shí): 包左不包右 console.log( str.substring(0,3) ); // 'ABC' // 不支持負(fù)數(shù),如果是負(fù)數(shù)則視為0 console.log( str.substring( -11, 3) ); // 'ABC' // 前大后小,則智能調(diào)換位置 console.log( str.substring(6,3) ); // '123' 總結(jié): a). 不改變?cè)瓟?shù)據(jù),返回值為截取的內(nèi)容。 b). 不支持負(fù)數(shù),如果是負(fù)數(shù)則視為0 c). 兩個(gè)參數(shù)時(shí),包左不包右,如果前大后小,則智能調(diào)換位置。

字符串替換:
1. str.replace(regExp/substr, 替換的內(nèi)容);

var str = "Today is a good day." // 字符串的形式: 只能替換一個(gè), 無法忽略大小寫 cosnole.log( str.replace('Today', 'Tomorrow') );// "Tomorrow is a good day." // 正則的形式: 可以替換多個(gè),可以忽略大小寫 console.log( str.replace(/today/ig, 'Tomorrow') ); // 傳入函數(shù)時(shí)的用法 str.replace(regExp/substr, function(a, b, c){// 可以傳3個(gè)參數(shù)// a: 為匹配的字符串// b: 為匹配字符串的起始位置// c: 為調(diào)用replace方法的字符串本身 });

字符串大小寫轉(zhuǎn)換:

var str = 'ABC123你好!'; str.toLowerCase(); // 英文字符轉(zhuǎn)換成小寫 var str = 'abc123你好!'; str.toUpperCase(); // 英文字符轉(zhuǎn)化成大寫

字符串分割: split() 和 join() 是一對(duì)

str.split(分隔符[,分割長度]); // 用于把一個(gè)字符串分割成字符串?dāng)?shù)組 var str = 'aaa|bbb|ccc'; // 按'|'進(jìn)行分割,參數(shù)不會(huì)出現(xiàn)在數(shù)組中; console.log( str.split('|') ); // ["aaa","bbb","ccc"] // 不帶參數(shù),整體作為一個(gè)元素 console.log( str.split() ); // ["aaa|bbb|ccc"] // 按空字符串分割,則每個(gè)字符都為一個(gè)元素 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í)行全局匹配,匹配成功則不再匹配

var str = 'ABC123你好!'; str.search(/3/i); // 5 str.search(/D/i); // -1

2. str.match(substr/regexp); // 返回匹配結(jié)果的數(shù)組,匹配失敗返回null

var str = 'A123B345ABC456"; // 在字符串內(nèi)檢索指定的值,或找到一個(gè)或多個(gè)匹配 console.log( str.match(/A/ig); // ["A","A"] // 未找到匹配內(nèi)容返回 null console.log( str.match(/DDD/ig); // null

3. 正則方法: 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); 返回一個(gè)數(shù)組,存放匹配結(jié)果,未找到,返回null


字符串案例:
1. 統(tǒng)計(jì)一個(gè)字符串中出現(xiàn)次數(shù)最多的字符和次數(shù)。
例如: "Hollow word! good day!" => o, 5次

2. 獲取URL中?后面的內(nèi)容,并轉(zhuǎn)化成對(duì)象的形式;
例如: "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

轉(zhuǎn)載于:https://www.cnblogs.com/yuxi2018/p/9531311.html

總結(jié)

以上是生活随笔為你收集整理的[记录] JavaScript 中的字符串操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。