JavaScript string charCodeAt() vs codePointAt() All In One
String.prototype.charCodeAt()
vsString.prototype.codePointAt()
String 值的 charCodeAt()
方法返回 0
到 65535
之間的整數
,表示給定索引
處的 UTF-16
程式碼單元。
取值範圍: [0, 2^16]
2 ** 16
// 65536
2 ** 16 - 1
// 65535
'h'.charCodeAt()
//104
'h'.charCodeAt(0)
//104
'h'.charCodeAt(1)
// NaN
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt
String 值的 codePointAt()
方法返回一個非負整數
,它是從給定索引
開始的字元的 Unicode
程式碼點值。
'h'.codePointAt()
// 104
'h'.codePointAt(0)
// 'h'.codePointAt()
'h'.codePointAt(1)
// undefined
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt
solutions
function scoreOfString(s: string): number {
let sum: number = 0;
for(let i = 0; i < s.length - 1; i++) {
sum += Math.abs(s.charCodeAt(i) - s.charCodeAt(i+1));
}
return sum;
};
// function scoreOfString(s: string): number {
// let sum: number = 0;
// for(let i = 0; i < s.length - 1; i++) {
// sum += Math.abs(s[i].charCodeAt(0) - s[i+1].charCodeAt(0));
// // sum += arr[i] = Math.abs(s[i].codePointAt(0) - s[i + 1].codePointAt(0));
// }
// return sum;
// };
demos
function scoreOfString(s: string): number {
let arr: number[] = new Array(s.length - 1).fill(0);
for(let i = 0; i < s.length - 1; i++) {
arr[i] = Math.abs(s[i].charCodeAt(0) - s[i+1].charCodeAt(0));
// arr[i] = Math.abs(s[i].codePointAt(0) - s[i + 1].codePointAt(0));
}
return arr.reduce((sum, i) => sum += i, 0);
};
/*
Line 5: Char 20: error TS2554: Expected 1 arguments, but got 0.
'h'.charCodeAt()
104
'h'.charCodeAt(0)
104
'h'.charCodeAt(1)
NaN
'h'.codePointAt()
104
'h'.codePointAt(0)
104
'h'.codePointAt(1)
undefined
*/
/*
'h'.codePointAt()
104
'h'.charCodeAt()
104
*/
https://leetcode.com/problems/score-of-a-string/?envType=daily-question&envId=2024-06-01
function scoreOfString(s: string): number {
const dict = {};
// const begin = `a`.charCodeAt(0);
// const end = `z`.charCodeAt(0);
// for (let i = begin; i <= end; i++) {
for (let i = 97; i <= 122; i++) {
dict[String.fromCodePoint(i)] = i;
// dict[String.fromCharCode(i)] = i;
}
// console.log(`dict =`, dict);
let arr: number[] = new Array(s.length - 1).fill(0);
for(let i = 0; i < s.length - 1; i++) {
arr[i] = Math.abs(dict[s[i]] - dict[s[i + 1]]);
}
return arr.reduce((sum, i) => sum += i, 0);
};
/*
const begin = `A`.charCodeAt();
const end = `z`.charCodeAt();
let dict = {};
for (let i = begin; i <= end; i++) {
dict[String.fromCodePoint(i)] = i;
}
console.log(`dict =`, dict);
// {A: 65, ..., y: 121}
const begin = `A`.charCodeAt();
const end = `z`.charCodeAt();
let dict = {};
for (let i = begin; i <= end; i++) {
dict[i] = String.fromCodePoint(i)
}
console.log(`dict =`, dict);
// {65: 'A', ..., 121: y}
*/
(🐞 反爬蟲測試!打擊盜版⚠️)如果你看到這個資訊, 說明這是一篇剽竊的文章,請訪問 https://www.cnblogs.com/xgqfrms/ 檢視原創文章!
String.fromCodePoint
convert
number
orstring
toASCII
String.fromCodePoint(65);
//"A"
String.fromCodePoint(`65`);
//"A"
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint
String.fromCharCode
String.fromCharCode(65)
// "A"
String.fromCharCode(`65`);
// "A"
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode
refs
convert number or string to ASCII in JavaScript All In One
https://www.cnblogs.com/xgqfrms/p/13333814.html
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 釋出文章使用:只允許註冊使用者才可以訪問!
原創文章,版權所有©️xgqfrms, 禁止轉載 🈲️,侵權必究⚠️!