了解 JavaScript 字串的屬性和方法
String 物件有一個靜態方法 String.fromCharCode(),可以用來從一系列的 Unicode 字元建立字串表示。以下是使用 ASCII 碼建立簡單字串的範例:
String.fromCodePoint(70, 108, 97, 118, 105, 111) //'Flavio'
你也可以使用八進制或十六進制數字:
String.fromCodePoint(0x46, 0154, parseInt(141, 8), 118, 105, 111) //'Flavio'
接下來介紹的所有方法都是實例方法:指的是要針對字串類型運行的方法。
實例方法
字串提供了一些獨特的方法可供使用:
charAt(i)charCodeAt(i)codePointAt(i)concat(str)endsWith(str)includes(str)indexOf(str)lastIndexOf(str)localeCompare()match(regex)normalize()padEnd()padStart()repeat()replace(str1, str2)search(str)slice(begin, end)split(separator)startsWith(str)substring()toLocaleLowerCase()toLocaleUpperCase()toLowerCase()toString()toUpperCase()trim()trimEnd()trimStart()valueOf()