[] === charAt
數組
let str = "abcdefg";
console.log(str[0]);//"a"
console.log(str.charAt(1));//"b"
複製代碼
str.length
ui
let str = "abcdefg";
console.log(str.length);//7
console.log(str[str.length-1]);//"g"
複製代碼
indexOf()
spa
let str = "abcdefg";
console.log(str.indexOf("a"));//0
console.log(str.indexOf("z"));//-1
複製代碼
charCodeAt()
、String.fromCharCode()
code
let str = "abcdefg";
console.log(str.charCodeAt(0));//"a" --> 97
console.log(String.fromCharCode(97));//97 --> "a"
複製代碼
concat()
排序
let str = "abc";
console.log(str.concat("efg"));//"abcefg"
console.log(str.concat("efg","hijk"));//"abcefghijk"
複製代碼
slice()
substring()
substr()
let str = "abcdefg";
/*slice*/
console.log(str.slice(1,6));//"bcdef" [1,6)
console.log(str.slice(1)); //"bcdefg" [1,str.length-1]
console.log(str.slice());//"abcdefg" [0,str.length-1]
console.log(str.slice(-2));//"fg"
console.log(str.slice(6, 1));//""
/* slice只傳兩個值個值,則表示左閉右開,不包括結束位置的值 傳入一個值,則表示,才起始位開始,把字符串切割完 不傳值,則切割整個字符串 也能夠傳入負數,字符串倒數第一位爲-1 */
/*substring*/
console.log(str.substring(1,6));//"bcdef" [1,6)
console.log(str.substring(1)); //"bcdefg" [1,str.length-1]
console.log(str.substring());//"abcdefg" [0,str.length-1]
console.log(str.substring(6,1));//"bcdef" [1,6)
console.log(str.substring(-1));//"abcdefg"
/* 用法和slice基本相同,只不過substring,傳入的第一個值比第二個值大 時,默認會進行從小值切到大值的處理 以及傳入負數,會切割整個字符串 */
/*substr(start,length)*/
console.log(str.substr(1,6));//"bcdefg" 6表明切割的length
console.log(str.substr(1)); //"bcdefg" [1,str.length-1]
console.log(str.substr());//"abcdefg" [0,str.length-1]
console.log(str.substr(-1));//"g"
/* substr 第一個參數表示開始切割的起始位置 第二個參數表示切割的長度 傳一個值和不傳值的表現和slice相同 也能夠傳入負值 */
複製代碼
toUpperCase()
toLowerCase()
let str1 = "adABDndj";
console.log(str1.toUpperCase());//"ADABDNDJ"
console.log(str1.toLowerCase());//"adabdndj"
複製代碼
split()
此方法和數組的arr.join()
對照着講索引
注意:字符串的全部方法,都不會改變字符串自己字符串
數組使用下標取值[idx]
string
let arr = [1,2,3,4];
console.log(arr[1]);//2
複製代碼
arr.concat()
it
let arr = [1,2,3];
console.log(arr.concat([4,5,6],[7,8,9]));//[1, 2, 3, 4, 5, 6, 7, 8, 9]
複製代碼
arr.length
io
slice
進行數組的切割用法和字符串相同
let arr = [1,2,3,4,5,6,7,8];
console.log(arr.slice(1,6));//[2, 3, 4, 5, 6] [1,6)
console.log(arr.slice(1)); //[2, 3, 4, 5, 6, 7, 8] [1,arr.length-1]
console.log(arr.slice());//[1, 2, 3, 4, 5, 6, 7, 8] [0,arr.length-1]
console.log(arr.slice(-2));//[7, 8]
console.log(arr.slice(6, 1));//[]
複製代碼
indexOf
查找let arr = [1,2,3,4,5,6,7,8];
console.log(arr.indexOf(8));//7
console.log(arr.indexOf(10));//-1
複製代碼
push
往數組末尾添加數據unshift
往數組開頭添加數據pop
刪除數組末尾的數據shift
刪除數組開頭第一個數據let arr = [1,3,4,5];
arr.push("a",6);
console.log(arr);//[1,3,4,5,"a",6];
arr.pop();
console.log(arr);//[1,3,4,5,"a"];
arr.unshift(2);
console.log(arr);//[2,1,3,4,5,"a"];
arr.shift();
console.log(arr);//[1,3,4,5,"a"];
複製代碼
arr.splice()
能夠實現上述四個方法的全部功能let arr = [1,3,4,5];
arr.splice(0,0,"12");//===arr.unshift("12");
console.log(arr);//["12", 1, 3, 4, 5]
arr.splice(0,1);//=== arr.shift();
console.log(arr);//[1, 3, 4, 5]
arr.splice(arr.length,0,"a");//===arr.push("a");
console.log(arr);//[1, 3, 4, 5, "a"]
arr.splice(arr.length-1,1);//===arr.pop();
console.log(arr);//[1, 3, 4, 5]
複製代碼
arr.sort()
let arr = [97,3,67,42,100,21];
//升序排序
arr.sort((a,b)=>a-b);
console.log(arr);//[3, 21, 42, 67, 97, 100]
//降序排序
arr.sort((a,b)=>b-a);
console.log(arr);//[100, 97, 67, 42, 21, 3]
複製代碼
arr.join()
let arr = [1,23,4,45,5];
let str = arr.join("-");
console.log(str);//"1-23-4-45-5"
//字符串切割成數組
let arr1= str.split("-");//參數表示以什麼字符爲界限切割字符串
console.log(arr1);//["1", "23", "4", "45", "5"]
複製代碼
arr.reverse()
let arr = [1,3,4,5,6,7];
arr.reverse();
console.log(arr);//[7, 6, 5, 4, 3, 1]
複製代碼
注意:數組中和字符串同名的方法,還有外加一個join()
都不改變原數組