js charAt & charCodeAt All In One


js charAt & charCodeAt All In One

charAt

String 对象的 charAt() 方法返回一个新字符串,该字符串由位于字符串中指定偏移量的单个 UTF-16 代码单元组成。

const str = 'ABCabc';

console.log(`str.charAt(0) = ${str.charAt(0)}`);
// "str.charAt(0) = A"

console.log(`str.charAt(3) = ${str.charAt(3)}`);
// "str.charAt(3) = a"

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

charCodeAt

charCodeAt() 方法返回一个介于 0 和 65535 之间的整数,表示给定索引处的 UTF-16 代码单元。


const str = 'ABCabc';

console.log(`str.charCodeAt(0) = ${str.charCodeAt(0)}`);
// "str.charCodeAt(0) = 65"

console.log(`str.charCodeAt(3) = ${str.charCodeAt(3)}`);
// "str.charCodeAt(3) = 97"

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

ArrayBuffer

TypedArray to Blob URL



function typedArrayToBlobURL(typedArray, mimeType) {
  return URL.createObjectURL(new Blob([typedArray.buffer], {type: mimeType}))
}

const bytes = new Uint8Array(59);
// Uint8Array(59) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, buffer: ArrayBuffer(59), byteLength: 59, byteOffset: 0, length: 59, Symbol(Symbol.toStringTag): 'Uint8Array']

for(let i = 0; i < 59; i++) {
  bytes[i] = 32 + i;
}
// Uint8Array(59) [32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, buffer: ArrayBuffer(59), byteLength: 59, byteOffset: 0, length: 59, Symbol(Symbol.toStringTag): 'Uint8Array']

const url = typedArrayToBlobURL(bytes, 'text/plain');
// !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ

const link = document.createElement('a');
link.href = url;
link.innerText = 'Open the arrayBuffer Blob URL';
document.body.appendChild(link);


refs


Flag Counter

?xgqfrms 2012-2020

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

原创文章,版权所有??xgqfrms, 禁止转载 ???,侵权必究??!