標籤:
var str="hello world";
// 1 charAt() 返回字串的第幾個下標字元
console.log(str.charAt(4));
// 2:fixed() 以打字機字型顯示字串
document.write(str.fixed());
// 3:indexOf()返回某個字元或字串片段在字串中首次出現的位置下標,不存在則會返回-1
console.log(str.indexOf("w"));
console.log(str.indexOf("pp"))
// 4:把字串轉化為大小寫
console.log(str.toUpperCase())//大寫
console.log(str.toLowerCase())//小寫
// 5:substring(star,stop) 方法用於提取字串中介於兩個指定下標之間的字元。
// 一個新的字串,該字串值包含 stringObject 的一個子字串,其內容是從 start 處到 stop-1 處的所有字元,其長度為 stop 減 start
// 。
console.log(str.substring(4))//從第4個下標開始,到字串最結尾
console.log(str.substring(2,5))//返回下標2到下標4之間字元
// 6: 把字串顯示為上 下標
document.write(str.sup())//上標
document.write(str.sub())//下標
// 7:將字串顯示為刪除線
document.write(str.strike());
// 8:split, 將字串分割成數組
console.log(str.split(" "));
// ["hello", "world"]
// 9 slice(star,end),提取字串片段
// 包括字串 stringObject 從 start 開始(包括 start)到 end 結束(不包括 end)為止的所有字元。star 允許取負數,取負數則為從末尾開始算
document.write(str.slice(3,6))
// 10 replace(a,b) a為匹配字串的正則規則, b為需要替換為的字串
console.log(str.replace(/hello/,"fuckb"));
js基礎複習---字串操作