[@@iterator]【イテレータ作成】
charAt【指定位置の文字】
at【インデックス位置要素 取得】
charCodeAt【指定位置のUnicode値】
codePointAt【指定位置のコードポイント値】
String.prototype.[@@iterator]【イテレータ作成】
メモ
概要
- コードポイント値のイテレータオブジェクト作成
- for-of【プロパティ値反復処理】が利用可能
- 順に処理することで、サロゲートペアの処理が可能
外部リンク
- ECMA-262 (英語)
String.prototype [ @@iterator ] ( ) ES2024 (15) ES2023 (14) ES2022 (13) Iteration ES2024 (15) ES2023 (14) ES2022 (13)
構文
string[ Symbol.iterator ]( )
イテレータオブジェクト
for (const char of string ) {
char の処理
}
例
const str1 = "text";
const strIter1 = str1[Symbol.iterator]();
console.log(strIter1.next().value); // 出力:t
console.log(strIter1.next().value); // 出力:e
console.log(strIter1.next().value); // 出力:x
console.log(strIter1.next().value); // 出力:t
console.log(strIter1.next().value); // 出力:undefined
for (const char of str1) {
console.log(char, "0x" + char.codePointAt(0).toString(16));
}
// 出力:
// t 0x74
// e 0x65
// x 0x78
// t 0x74
const str2 = "テキスト";
const strIter2 = str2[Symbol.iterator]();
console.log(strIter2.next().value); // 出力:テ
console.log(strIter2.next().value); // 出力:キ
console.log(strIter2.next().value); // 出力:ス
console.log(strIter2.next().value); // 出力:ト
console.log(strIter2.next().value); // 出力:undefined
for (const char of str2) {
console.log(char, "0x" + char.codePointAt(0).toString(16));
}
// 出力:
// テ 0x30c6
// キ 0x30ad
// ス 0x30b9
// ト 0x30c8
const str3 = "𠮟る"; // 𠮟:U+20B9F(第3水準) る:U+308B
const strIter3 = str3[Symbol.iterator]();
console.log(strIter3.next().value); // 出力:𠮟
console.log(strIter3.next().value); // 出力:る
console.log(strIter3.next().value); // 出力:undefined
for (const char of str3) {
console.log(char, "0x" + char.codePointAt(0).toString(16));
}
// 出力:
// 𠮟 0x20b9f
// る 0x308b
String.prototype.charAt【指定位置の文字】
String.prototype.at【インデックス位置要素 取得】
メモ
概要
- 指定位置の文字を取得
- at【インデックス位置要素 取得】 は負数指定も可 (末尾から)
- [ ]【文字アクセス】 でも可能
- サロゲートペア(4バイト)は未対応
- for-of・[@@iterator]【イテレータ作成】で対応
関連
外部リンク
- ECMA-262 (英語)
String.prototype.charAt (pos) ES2024 (15) ES2023 (14) ES2022 (13) String.prototype.at ( index ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
string.charAt( pos )
指定位置の文字 (空文字:範囲外)
pos 位置 (0 ~【文字列長】- 1)
string.at( index )
インデックス位置の文字 (undefined:範囲外)
index インデックス位置
0 ≦先頭から
< 0末尾から
例
let str = "text";
console.log(str[2], str.charAt(2), str.at(2), str.at(-2));
// 出力:x x x x
str = "テキスト";
console.log(str[2], str.charAt(2), str.at(2), str.at(-2));
// 出力:ス ス ス ス
// 範囲外
console.log(str[4]);
// 出力:undefined
console.log(`[${str.charAt(4)}]`);
// 出力:[]
console.log(`[${str.at(4)}]`);
// 出力:[undefined]
console.log(`[${str.at(-5)}]`);
// 出力:[undefined]
// サロゲートペア
str = "𠮟る"; // 𠮟:U+20B9F(第3水準) る:U+308B
console.log(str.length);
// 出力:3
for (let i = 0; i < str.length; i++) {
console.log(i, str[i], str.charAt(i), str.at(i));
}
// 出力:
// 0 [不正文字] [不正文字] [不正文字]
// 1 [不正文字] [不正文字] [不正文字]
// 2 る る る
// for-of (参考)
for (const char of str) {
console.log(char, "0x" + char.codePointAt(0).toString(16));
}
// 出力:
// 𠮟 0x20b9f
// る 0x308b
String.prototype.charCodeAt【指定位置のUnicode値】
メモ
概要
- 指定位置のUnicode値を取得
- サロゲートペア(4バイト)は未対応
- for-of・[@@iterator]【イテレータ作成】で対応
- Unicode値から文字列:fromCharCode【Unicode値から文字列生成】
関連
外部リンク
- ECMA-262 (英語)
String.prototype.charCodeAt (pos) ES2024 (15) ES2023 (14) ES2022 (13)
構文
string.charCodeAt( pos )
指定位置のUnicode値 (範囲外:NaN)
pos 位置 (0 ~【文字列長】- 1)
例
let str = "text";
let char = str.charCodeAt(2);
console.log("0x" + char.toString(16), String.fromCharCode(char));
// 出力:0x78 x
str = "テキスト";
char = str.charCodeAt(2);
console.log("0x" + char.toString(16), String.fromCharCode(char));
// 出力:0x30b9 ス
// 範囲外
console.log(str.charCodeAt(4));
// 出力:NaN
// サロゲートペア
str = "𠮟る"; // 𠮟:U+20B9F(第3水準) る:U+308B
console.log(str.length);
// 出力:3
for (let i = 0; i < str.length; i++) {
char = str.charCodeAt(i);
console.log(i, "0x" + char.toString(16), String.fromCharCode(char));
}
// 出力:
// 0 0xd842 [不正文字]
// 1 0xdf9f [不正文字]
// 2 0x308b る
// for-of (参考)
for (const char of str) {
console.log(char, "0x" + char.codePointAt(0).toString(16));
}
// 出力:
// 𠮟 0x20b9f
// る 0x308b
String.prototype.codePointAt【指定位置のコードポイント値】
メモ
概要
- 指定位置のコードポイント値を取得
- コードポイント値 < 0x110000
- サロゲートペア(4バイト)は不備あり (for-of・[@@iterator]【イテレータ作成】で対応)
- コードポイント値から文字列: fromCodePoint【コードポイント値から文字列生成】
関連
外部リンク
- ECMA-262 (英語)
String.prototype.codePointAt ( pos ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
string.codePointAt( pos )
指定位置のコードポイント値 (範囲外:undefined)
pos 位置 (0 ~【文字列長】- 1)
例
let str = "text";
let char = str.codePointAt(2);
console.log("0x" + char.toString(16) + " " + String.fromCharCode(char));
// 出力:0x78 x
str = "テキスト";
char = str.codePointAt(2);
console.log("0x" + char.toString(16) + " " + String.fromCharCode(char));
// 出力:0x30b9 ス
// 範囲外
console.log(str.codePointAt(4));
// 出力:NaN
// サロゲートペア
str = "𠮟る"; // 𠮟:U+20B9F(第3水準) る:U+308B
console.log(str.length);
// 出力:3
for (let i = 0; i < str.length; i++) {
char = str.codePointAt(i);
console.log(i, "0x" + char.toString(16), String.fromCodePoint(char));
}
// 出力:
// 0 0x20b9f 𠮟
// 1 0xdf9f [不正文字]
// 2 0x308b る
// for-of (参考)
for (const char of str) {
console.log(char, "0x" + char.codePointAt(0).toString(16));
}
// 出力:
// 𠮟 0x20b9f
// る 0x308b