JS常用方法總結--String字串篇
個人學習筆記
String字串
一、增
1、repeat() 方法:字串複製指定次數,返回值是字串。
用法:string.repeat(count)
var str = "HELLO WORLD"; var nstr1 = str.repeat(2); // 複製指定次數 console.log(str); // HELLO WORLD console.log(nstr1); // HELLO WORLDHELLO WORLD console.log(typeof(nstr1)); // string
二、刪
1、trim() 方法:用於刪除字串的頭尾空白符,空白符包括:空格、製表符 tab、換行符等其他空白符等。
用法:string.trim()
var str = " HELLO WORLD hello world "; var nstr1 = str.trim(); // 去除前後空格 console.log(str); // HELLO WORLD hello world console.log(nstr1); // HELLO WORLD HELLO WORLD console.log(typeof(nstr1)); // string
2、replace() 方法:用於在字串中用一些字元替換另一些字元(只替換一個),或替換一個與正則表示式匹配的子串(替換全部),返回值是字串。
用法:string.replace(searchvalue,newvalue)var str = "HELLO WORLD hello world"; var nstr0 = str.replace(" ",""); // 去除 字元間的 空格 var nstr1 = str.replace("world","javascript"); // 替換字串 var nstr2 = str.replace(/world/g,"javascript"); //通過正則 替換字串 var nstr3 = str.replace(/world/gi,"javascript"); // 通過正則 替換字串 console.log(str); // HELLO WORLD hello world console.log(nstr0); // HELLOWORLD hello world console.log(nstr1); // HELLO WORLD hello javascript console.log(typeof(nstr1)); // string console.log(nstr2); // HELLO WORLD hello javascript console.log(nstr3); // HELLO javascript hello javascript
3、replaceAll() 方法:類似於replace方法,不過他會替換所有匹配到的子字串。
var str = "HELLO WORLD hello world"; var nstr0 = str.replaceAll(" ",""); // 去除 字元間的 空格 console.log(str); // HELLO WORLD hello world console.log(nstr0); // HELLOWORLDhelloworld
三、改
1、toLowerCase() 方法用於把字串轉換為小寫。
用法:string.toLowerCase()
var str = "HELLO WORLD hello world"; var nstr1 = str.toLowerCase(); console.log(str); // HELLO WORLD hello world console.log(nstr1); // hello world hello world console.log(typeof(nstr1)); // string
2、toUpperCase() 方法:用於把字串轉換為大寫。
用法:string.toUpperCase()
var str = "HELLO WORLD hello world"; var nstr1 = str.toUpperCase(); console.log(str); // HELLO WORLD hello world console.log(nstr1); // HELLO WORLD HELLO WORLD console.log(typeof(nstr1)); // string
四、查
1、charAt() 方法:可返回指定位置的字元,返回值是字串。
用法:string.charAt(index)
var str = "HELLO WORLD"; var nstr = str.charAt(2); console.log(nstr); // L console.log(typeof(nstr)); // string
2、charCodeAt() 方法:可返回指定位置的字元的 Unicode 編碼,返回值是 0 - 65535 之間的整數,返回值是整數數字。
用法:string.charCodeAt(index)
var str = "HELLO WORLD"; var nstr = str.charCodeAt(2); console.log(nstr); // 76 console.log(typeof(nstr)); // number
3、fromCharCode() 可接受一個指定的 Unicode 值,然後返回一個字串,返回值是字串。
用法:String.fromCharCode(n1,n2, ...,nX)
var nstr = String.fromCharCode(76); console.log(nstr); // L console.log(typeof(nstr)); // string
4、indexOf() 方法:可返回某個指定的字串值在字串中首次出現的位置,返回值是整數數字。
用法:string.indexOf(searchvalue,[start])
var str = "HELLO WORLD"; var nstr1 = str.indexOf("L"); var nstr2 = str.indexOf("L",4); console.log(nstr1); // 2 console.log(typeof(nstr1)); // number console.log(nstr2); // 9 console.log(typeof(nstr2)); // number
5、includes() 方法:用於判斷字串是否包含指定的子字串,返回值是布林值。
用法:string.includes(searchvalue,[start])
var str = "HELLO WORLD"; var nstr1 = str.includes("O"); var nstr2 = str.includes("O",8); console.log(nstr1); // true console.log(typeof(nstr1)); // boolean console.log(nstr2); // false console.log(typeof(nstr2)); // boolean
6、match() 方法:可在字串內檢索指定的值,或找到一個或多個正則表示式的匹配,返回值是陣列。
用法:string.match(regexp)
var str = "HELLO WORLD hello world"; ; var nstr1 = str.match(/L/g); // g 修飾符 var nstr2 = str.match(/L/gi); // i 修飾符 console.log(nstr1); // ["L", "L", "L"] console.log(typeof(nstr1)); // object console.log(nstr2); // ["L", "L", "L", "l", "l", "l"] console.log(typeof(nstr2)); // object
7、search() 方法:用於檢索字串中指定的子字串,或檢索與正則表示式相匹配的子字串,返回值是數值。
用法:string.search(searchvalue)
var str = "HELLO WORLD hello world HELLO world"; var nstr1 = str.search(/world/g,"O"); // g 修飾符 全域性 var nstr2 = str.search(/world/gi,"o"); // i 修飾符 忽略大小寫 console.log(nstr1); // 18 console.log(typeof(nstr1)); // number console.log(nstr2); // 6 console.log(typeof(nstr2)); // number
8、slice() 方法:可提取字串的某個部分,並以新的字串返回被提取的部分,返回值是字串。
用法:string.slice(start,[end])
var str = "HELLO WORLD hello world"; var nstr1 = str.slice(1,10); console.log(str); // HELLO WORLD hello world console.log(nstr1); // ELLO WORL console.log(typeof(nstr1)); // string
9、substr() 方法:可在字串中抽取從開始下標 到指定數目的字元,返回值是字串。
用法:string.substr(start,[length])
var str = "HELLO WORLD hello world"; var nstr1 = str.substr(1,10); console.log(str); // HELLO WORLD hello world console.log(nstr1); // ELLO WORLD console.log(typeof(nstr1)); // string
10、substring() 方法:用於提取字串中介於兩個指定下標之間的字元,返回值是字串。
用法:string.substring(start,[end])
var str = "HELLO WORLD hello world"; var nstr1 = str.substring(1,10); console.log(str); // HELLO WORLD hello world console.log(nstr1); // ELLO WORL console.log(typeof(nstr1)); // string
五、轉換
1、split() 方法:用於把一個字串分割成字串陣列。
用法:string.split([separator],[limit])
separator:字串或正則表示式,從該引數指定的地方分割 string Object。
limit:該引數可指定返回的陣列的最大長度。如果設定了該引數,返回的子串不會多於這個引數指定的陣列。如果沒有設定該引數,整個字串都會被分割,不考慮它的長度。
var str = "HELLO WORLD hello world HELLO world"; var str0 = "HELLO WORLD hello world HELLO world"; var nstr1 = str.split("or"); var nstr2 = str0.split(/or/g); // g修飾符 全域性 var nstr3 = str0.split(/or/gi); // i修飾符 忽略大小寫 console.log(nstr1); // ["HELLO WORLD hello w", "ld HELLO w", "ld"] console.log(typeof(nstr1)); // object console.log(nstr2); // ["HELLO WORLD hello w", "ld HELLO w", "ld"] console.log(typeof(nstr2)); // object console.log(nstr3); // ["HELLO W", "LD hello w", "ld HELLO w", "ld"]
2、concat() 方法:用於連線兩個或多個字串,返回值是字串。
用法:string.concat(string1,string2, ...,stringX)
var str1 = "HELLO WORLD"; var str2 = "I'm coming"; var nstr = str1.concat(str2); console.log(nstr); // HELLO WORLDI'm coming console.log(typeof(nstr)); // string