Metode JavaScript String fromCharCode () atgriež virkni, kas izveidota no norādītās UTF-16 kodu vienību secības.
Metodes sintakse fromCharCode()
ir šāda:
String.fromCharCode(num1,… , numN)
fromCharCode()
Metodi, kas ir statiska metode, sauc izmantojot String
klases nosaukumu.
fromCharCode () parametri
fromCharCode()
Metode ņem in:
- num1,…, numN - UTF-16 kodu vienību secība (skaitļi no 0 līdz 65535 ). Skaitļi, kas pārsniedz 65535 (0xFFFF), tiek saīsināti.
Atgriezt vērtību no fromCharCode ()
- Atgriež virkni N garumā, kas sastāv no N norādītajām UTF-16 kodu vienībām.
Piezīme . fromCharCode()
Metode atgriež virkni, nevis String
objektu.
Piemērs: Izmantojot metodi fromCharCode ()
// most common characters can be represented by single 16 bit value let string1 = String.fromCharCode(65, 66, 67); console.log(string1); // ABC // numbers can be passed as a hexadecimal value let string2 = String.fromCharCode(0x2014); console.log(string2); // - // num> 65535 are truncated, so here 1 is truncated // equivalent to 0x2014 let string3 = String.fromCharCode(0x12014); console.log(string3); // -
Rezultāts
ABC - -
Tomēr UTF-16 papildu rakstzīmēm ir nepieciešamas divas kodu vienības.
String.fromCharCode(0xD83C, 0xDF03); // Code Point U+1F303 "Night with String.fromCharCode(55356, 57091); // Stars" == "uD83CuDF03"
Lai apskatītu visu UTF-16 tabulu, apmeklējiet ASCII tabulu.
Ieteicamā literatūra:
- JavaScript virkne charCodeAt ()