xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

JavaScript string charCodeAt() vs codePointAt() All In One

JavaScript string charCodeAt() vs codePointAt() All In One

String.prototype.charCodeAt() vs String.prototype.codePointAt()

String 值的 charCodeAt() 方法返回 065535 之间的整数,表示给定索引处的 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

 */

image

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}

 */


String.fromCodePoint

convert number or string to ASCII


String.fromCodePoint(65);
//"A"
String.fromCodePoint(`65`);
//"A"

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint

image

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

image

https://www.cnblogs.com/xgqfrms/p/13333814.html



©xgqfrms 2012-2025

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2023-06-05 cnblogs 停更, 迁移到 GitHub SSR website All In One
2022-06-05 ES6 Map Object index All In One
2021-06-05 Dell U2720QM All In One
2020-06-05 css animation & animationend event & onanimationend
2020-06-05 css animation & animation-fill-mode
2020-06-05 可视化代码生成器 All In One
2020-06-05 如何取消 Google Cloud Platform 试用 & 关闭 GCP 帐号 & 删除信用卡 & 取消订阅 All In One
点击右上角即可分享
微信分享提示