A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String/charAt below:

String.prototype.charAt() - JavaScript | MDN

String.prototype.charAt()

Baseline Widely available

charAt() は String 値のメソッドは、指定されたインデックスにある単一の UTF-16 コード単位からなる新しい文字列を返します。

charAt() は常に文字列を UTF-16 コード単位の並びとして索引付けます。そのため、孤立したサロゲートを返す可能性があります。指定されたインデックスの完全な Unicode コードポイントを取得するには、String.prototype.codePointAt() および String.fromCodePoint() を使用してください。

試してみましょう
const sentence = "The quick brown fox jumps over the lazy dog.";

const index = 4;

console.log(`The character at index ${index} is ${sentence.charAt(index)}`);
// Expected output: "The character at index 4 is q"
構文 引数
index

返される文字のゼロから始まる文字のインデックスです。整数に変換されます。undefined は 0 に変換されます。

返値

指定された index の位置にある文字(厳密に 1 つの UTF-16 コードポイント)を表す文字列です。index の位置が文字列の範囲を外れていた場合は、charAt() は空文字列を返します。

解説

文字列の中の文字は、左から右に向けてインデックス付けされています。最初の文字の添字は 0 であり、最後の文字の添字は str という名前の文字列であれば str.length - 1 です。

Unicode のコードポイントは 0 から 1114111 (0x10FFFF) までの範囲です。charAt() は常に 65536 より小さい値を返しますが、これは上位のコードポイントは 16 ビットのサロゲート擬似文字のペアによって表されているからです。したがって、65535 より大きい値を持つ完全な文字を取得するには、charAt(i) だけでなく charAt(i + 1) も取得するか(2 つの文字を持つ文字列を操作する場合と同じです)、または codePointAt(i) と String.fromCodePoint() を使用する必要があります。Unicode に関する情報はUTF-16 文字、Unicode コードポイント、書記素クラスターを参照してください。

charAt() は ブラケット記法を使用して指定された位置の文字にアクセスするのにとても似ています。主な違いは次の通りです。

例 文字列の中の様々な位置の文字の表示

次の例は、`"Brave new world"`` という文字列の中の様々な位置の文字を表示します。

const anyString = "Brave new world";
console.log(`The character at index 0   is '${anyString.charAt()}'`);
// No index was provided, used 0 as default

console.log(`The character at index 0   is '${anyString.charAt(0)}'`);
console.log(`The character at index 1   is '${anyString.charAt(1)}'`);
console.log(`The character at index 2   is '${anyString.charAt(2)}'`);
console.log(`The character at index 3   is '${anyString.charAt(3)}'`);
console.log(`The character at index 4   is '${anyString.charAt(4)}'`);
console.log(`The character at index 999 is '${anyString.charAt(999)}'`);

これらの行は以下のように表示されます。

The character at index 0   is 'B'

The character at index 0   is 'B'
The character at index 1   is 'r'
The character at index 2   is 'a'
The character at index 3   is 'v'
The character at index 4   is 'e'
The character at index 999 is ''

charAt() は妥当な Unicode 文字ではない、孤立サロゲートを返す可能性があります。

const str = "ð ®·ð ®¾";
console.log(str.charAt(0)); // "\ud842"、これは妥当な Unicode 文字ではない
console.log(str.charAt(1)); // "\udfb7"、これは妥当な Unicode 文字ではない

指定された位置の完全な Unicode コードポイントを取得するには、String.prototype.codePointAt() やスプレッド構文のように、Unicode コードポイントで分割して Unicode コードポイントの配列にするインデックスメソッドを使用してください

const str = "ð ®·ð ®¾";
console.log(String.fromCodePoint(str.codePointAt(0))); // "ð ®·"
console.log([...str][0]); // "ð ®·"

メモ: charAt() を使用して上記の解決策を再実装することは避けてください。孤立サロゲートの検出とそのペアリングは複雑で、文字列の内部表現を直接使用する組み込み API の方がパフォーマンスが高いかもしれません。必要であれば、上記の API のポリフィルをインストールしてください。

仕様書 ブラウザーの互換性 関連情報

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4