20 分鐘出結果,武漢大學研發出新冠病毒快檢新方法
阿新 • • 發佈:2022-04-03
1.字串擷取 substring()
//包含開始 不包含結束 (返回新字串,不改變原字串) let str = "123456"; console.log( str .substring(1,3) ); //23 console.log( str .substring(1, 100) ); //"23456" console.log( str .substring(1) ); //"23456" console.log(str ); //"123456"
2. 轉大小寫
let str = "ABCdef"; console.log( str.toLocaleUpperCase() );//ABCDEF
console.log( str.toLocaleLowerCase() ); //abcdef console.log( str ); //ABCdef
3.字串轉陣列 split()
let date = "2018-07-08"; console.log( date.split("-") ); //['2018', '07', '08'] let nums = "1,2,3,4,5"; console.log(nums.split(",")); //['1', '2', '3', '4', '5'] let str = "阿司法鑑定所"; console.log( str.split("") ); // ['阿', '司', '法', '鑑', '定', '所']
3.查詢字串 indexOf()
let str = "1234567"; /*返回引數字串最早出現在afei中的位置*/ console.log( str.indexOf("2") ); // 1 console.log( str.indexOf("0") ); // -1 console.log( str.indexOf("4") ); //3
4.字串擷取 slice(start,end)
擷取字串,start指定字串開始的位置,end不在擷取範圍之內 前包後不包 let str= "12345678"; console.log( str.slice(2, 6) ); //3456
5.字串替換 replace()
let str = '#home#home' let str1= 'home' let newStr = str.replace(str1, 'home1') //得到newStr1的結果為"#home1#home"