TextEncoder class

WHATWG Encoding StandardTextEncoder API の実装。 TextEncoder のすべてのインスタンスでは、UTF-8 エンコードのみがサポートされます。

const encoder = new TextEncoder();
const uint8array = encoder.encode('this is some data');

TextEncoder クラスは、グローバル オブジェクトでも使用できます。

プロパティ

encoding

TextEncoder インスタンスでサポートされるエンコード。 常に 'utf-8'に設定します。

メソッド

encode(string)

UTF-8 は、input 文字列をエンコードし、エンコードされたバイトを含む Uint8Array を返します。

encodeInto(string, Uint8Array)

UTF-8 は、src 文字列を dest Uint8Array にエンコードし、読み取られた Unicode コード単位と書き込まれた UTF-8 バイトを含むオブジェクトを返します。

const encoder = new TextEncoder();
const src = 'this is some data';
const dest = new Uint8Array(10);
const { read, written } = encoder.encodeInto(src, dest);

プロパティの詳細

encoding

TextEncoder インスタンスでサポートされるエンコード。 常に 'utf-8'に設定します。

encoding: string

プロパティ値

string

メソッドの詳細

encode(string)

UTF-8 は、input 文字列をエンコードし、エンコードされたバイトを含む Uint8Array を返します。

function encode(input?: string): Uint8Array

パラメーター

input

string

戻り値

Uint8Array

encodeInto(string, Uint8Array)

UTF-8 は、src 文字列を dest Uint8Array にエンコードし、読み取られた Unicode コード単位と書き込まれた UTF-8 バイトを含むオブジェクトを返します。

const encoder = new TextEncoder();
const src = 'this is some data';
const dest = new Uint8Array(10);
const { read, written } = encoder.encodeInto(src, dest);
function encodeInto(src: string, dest: Uint8Array): EncodeIntoResult

パラメーター

src

string

エンコードするテキスト。

dest

Uint8Array

エンコード結果を保持する配列。

戻り値