concat【文字列連結】
fromCharCode【Unicode値から文字列生成】
fromCodePoint【コードポイント値から文字列生成】
repeat【文字列の繰り返し】
String.prototype.concat【文字列連結】
メモ
概要
- 文字列に指定文字列を連結 (元の文字列は変更なし)
- 引数が文字列以外の場合、文字列に変換後連結
- 通常、連結演算子 (+ または +=) を使用
外部リンク
- ECMA-262 (英語)
String.prototype.concat ( ...args ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
string.concat( ...args )
連結後の文字列
args連結する文字列の並び
例
var str = "ABC";
var strNew = str.concat("def", "GHI");
console.log(strNew); // 出力:ABCdefGHI
console.log(str); // 出力:ABC
strNew = "ABC";
strNew += "def" + "GHI";
console.log(strNew); // 出力:ABCdefGHI
String.fromCharCode【Unicode値から文字列生成】
メモ
概要
- Unicode値から文字列生成
- 文字からUnicode値取得
外部リンク
- ECMA-262 (英語)
String.fromCharCode ( ...codeUnits ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
String.fromCharCode( ...codeUnits )
生成文字列 (引数省略:空文字列)
codeUnits各文字のUnicode値の並び
例
var str;
str = String.fromCharCode();
console.log("'" + str + "'"); // 出力:''
str = String.fromCharCode(0x41, 0x42, 0x43);
console.log("'" + str + "'"); // 出力:'ABC'
str = String.fromCharCode(0x3042, 0x3044, 0x3046);
console.log("'" + str + "'"); // 出力:'あいう'
String.fromCodePoint【コードポイント値から文字列生成】
メモ
概要
- コードポイント値から文字列生成
- 文字からコードポイント値取得
外部リンク
- ECMA-262 (英語)
String.fromCodePoint ( ...codePoints ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
String.fromCodePoint( ...codePoints )
生成文字列 (引数省略:空文字列)
codePointsコードポイント値の並び
RangeError
コードポイント値が NaN・undefined 等 数値変換できない
コードポイント値 < 0 または 0x10FFFF < コードポイント値
例
/**
* 文字列のコードポイント値 (codePointAt) を16進数の文字列に変換
* @param {string} 文字列
* @return {string} 16進数の文字列
*/
function code16(str) {
var str16 = "(";
for (var i = 0; i < str.length; i++) {
str16 += "0x" + str.codePointAt(i).toString(16) + " ";
}
str16 = str16.trim() + ")";
return str16;
}
var str;
str = String.fromCodePoint();
console.log(str, str.length, code16(str));
// 出力: 0 ()
str = String.fromCodePoint(0x41, 0x42, 0x43);
console.log(str, str.length, code16(str));
// 出力:ABC 3 (0x41 0x42 0x43)
str = String.fromCodePoint(0x3042, 0x3044, 0x3046);
console.log(str, str.length, code16(str));
// 出力:あいう 3 (0x3042 0x3044 0x3046)
str = String.fromCodePoint(0x53F1, 0x308B); // 叱:U+53F1 (第1水準)
console.log(str, str.length, code16(str));
// 出力:叱る 2 (0x53f1 0x308b)
str = String.fromCodePoint(0x20B9F, 0x308B); // 𠮟:U+20B9F(第3水準)
console.log(str, str.length, code16(str));
// 出力:𠮟る 3 (0x20b9f 0xdf9f 0x308b)
str = String.fromCodePoint(NaN); // RangeError 例外
str = String.fromCodePoint(-1); // RangeError 例外
str = String.fromCodePoint(0x110000); // RangeError 例外
String.prototype.repeat【文字列の繰り返し】
メモ
概要
- 文字列の繰り返しを作成 (元の文字列は変更なし)
外部リンク
- ECMA-262 (英語)
String.prototype.repeat ( count ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
string.repeat( count )
生成文字列 (count = 0:空文字列)
count繰り返し数
RangeError
count < 0
count :+∞
例
var str = "Abc";
var strNew = str.repeat(0);
console.log(strNew, strNew.length); // 出力: 0
strNew = str.repeat(3);
console.log(strNew, strNew.length); // 出力:AbcAbcAbc 9
console.log(str); // 出力:Abc