. .
UTF-16 .
.
, .
let single = '';
let double = "";
let backticks = ``;
. . ${} . (template literal) .
function sum(a, b) {
return a + b;
}
alert(`1 + 2 = ${sum(1, 2)}.`); // 1 + 2 = 3.
.
let guestList = `:
* John
* Pete
* Mary
`;
alert(guestList); //
. .
. .
let guestList = ": // Error: Invalid or unexpected token
* John";
. . .
' (template function)' . func`string` (func) , . ' (tagged template)' , . MDN . .
' (newline character)' \n .
let guestList = ":\n * John\n * Pete\n * Mary";
alert(guestList); //
.
let str1 = "Hello\nWorld"; // ' '
// ()
let str2 = `Hello
World`;
alert(str1 == str2); // true
.
:
\n |
|
\r |
(carriage return). Windows (\r\n) . . |
\', \" |
|
\\ |
|
\t |
|
\b, \f, \v |
(Backspace), (Form Feed), (Vertical Tab) . . |
\xXX |
16 XX (: 'z' '\x7A' ). |
\uXXXX |
UTF-16 16 XXXX . XXXX 16 (: \u00A9 ). |
\u{XXXXXXX}( 16 ) |
UTF-32 . 4 . . |
:
alert( "\u00A9" ); //
alert( "\u{20331}" ); // , ( )
alert( "\u{1F60D}" ); // , ( )
' (escape character)' (backslash character) \ .
.
:
alert( 'I\'m the Walrus!' ); // I'm the Walrus!
\ . .
. .
alert( `I'm the Walrus!` ); // I'm the Walrus!
\ \ . \ . alert .
\ ?
\\ .
alert( `: \\` ); // : \
length .
alert( `My\n`.length ); // 3
\n My\n 3.
length . str.length str.length() . .
length , . .
pos [pos] str.charAt(pos) . 0 .
let str = `Hello`;
//
alert( str[0] ); // H
alert( str.charAt(0) ); // H
//
alert( str[str.length - 1] ); // o
. charAt .
. [] undefined, charAt .
let str = `Hello`;
alert( str[1000] ); // undefined
alert( str.charAt(1000) ); // '' ( )
for..of .
for (let char of "Hello") {
alert(char); // H,e,l,l,o (char H, e, l, l, o .)
}
. .
.
let str = 'Hi';
str[0] = 'h'; // Error: Cannot assign to read only property '0' of string 'Hi'
alert( str[0] ); // .
, str .
:
let str = 'Hi';
str = 'h' + str[1]; //
alert( str ); // hi
.
toLowerCase() toUpperCase() , ( ).
alert( 'Interface'.toUpperCase() ); // INTERFACE
alert( 'Interface'.toLowerCase() ); // interface
.
alert( 'Interface'[0].toLowerCase() ); // 'i'
(substring) .
str.indexOf
str pos , substr . -1 .
:
let str = 'Widget with id';
alert( str.indexOf('Widget') ); // 0, str 'Widget'
alert( str.indexOf('widget') ); // -1, indexOf
alert( str.indexOf("id") ); // 1, "id" (Widget id)
str.indexOf(substr, pos) pos , .
"id" 1 , 2 "id" .
let str = 'Widget with id';
alert( str.indexOf('id', 2) ) // 12
indexOf . indexOf .
let str = 'As sly as a fox, as strong as an ox';
let target = 'as'; // as .
let pos = 0;
while (true) {
let foundPos = str.indexOf(target, pos);
if (foundPos == -1) break;
alert( `: ${foundPos}` );
pos = foundPos + 1; // .
}
.
let str = "As sly as a fox, as strong as an ox";
let target = "as";
let pos = -1;
while ((pos = str.indexOf(target, pos + 1)) != -1) {
alert( `: ${pos}` );
}
str.lastIndexOf(substr, position)str.lastIndexOf(substr, position) indexOf . .
.
if indexOf . .
let str = "Widget with id";
if (str.indexOf("Widget")) {
alert("!"); // .
}
str.indexOf("Widget") 0 , if 0 false alert .
-1 .
let str = "Widget with id";
if (str.indexOf("Widget") != -1) {
alert("!"); // .
}
NOT
(bitwise) NOT ~ . NOT 32 ( ) .
n 32 ~n -(n+1) .
:
alert( ~2 ); // -3, -(2+1)
alert( ~1 ); // -2, -(1+1)
alert( ~0 ); // -1, -(0+1)
alert( ~-1 ); // 0, -(-1+1)
32 n , ~n 0 n == -1 .
indexOf -1 if ( ~str.indexOf("...") ) .
~str.indexOf("...") .
let str = "Widget";
if (~str.indexOf("Widget")) {
alert( '!' ); // .
}
. .
if (~str.indexOf(...)) ' .
-1 ~ 0 . ~ 32 . 4294967295(~4294967295 0). ~ .
.includes ( ) . .
includes, startsWith, endsWith
str.includes(substr, pos) str substr true false .
.
alert( "Widget with id".includes("Widget") ); // true
alert( "Hello".includes("Bye") ); // false
str.includes str.indexOf .
alert( "Widget".includes("id") ); // true
alert( "Widget".includes("id", 3) ); // false, "id" .
str.startsWith str.endsWith str (start with) (end with) .
alert( "Widget".startsWith("Wid") ); // true, "Widget" "Wid" .
alert( "Widget".endsWith("get") ); // true, "Widget" "get" .
. substring, substr, slice .
str.slice(start [, end])-
startend(end) .:
let str = "stringify"; alert( str.slice(0, 5) ); // 'strin', 0 5 (5 ) alert( str.slice(0, 1) ); // 's', 0 1 (1 ), .
let str = "stringify"; alert( str.slice(2) ); // ringify, 2startend. .let str = "stringify"; // 4 1 alert( str.slice(-4, -1) ); // gif str.substring(start [, end])-
startend.substringslicestartend.:
let str = "stringify"; // . alert( str.substring(2, 6) ); // "ring" alert( str.substring(6, 2) ); // "ring" // slice . alert( str.slice(2, 6) ); // "ring" () alert( str.slice(6, 2) ); // "" ( )substring.0. str.substr(start [, length])-
startlength.substrsubstringslice.let str = "stringify"; alert( str.substr(2, 4) ); // ring,.
let str = "stringify"; alert( str.substr(-4, 2) ); // gi,
.
| () | ||
|---|---|---|
slice(start, end) |
start end(end ) |
|
substring(start, end) |
start end |
0 |
substr(start, length) |
start length |
. substr . substr (ECMA-262 ) , B(Annex B) . .
slice substring . . slice .
.
-
.
alert( 'a' > 'Z' ); // true -
(diacritical mark) .
alert( 'sterreich' > 'Zealand' ); // true (sterreich - ).
sterreichZealand.
.
UTF-16 , UTF-16 . .
str.codePointAt(pos)-
pos.// . alert( "z".codePointAt(0) ); // 122 alert( "Z".codePointAt(0) ); // 90 String.fromCodePoint(code)-
code.alert( String.fromCodePoint(90) ); // Z\u16 .// 90 16 5a. alert( '\u005a' ); // Z
65 220 ( ) .
let str = '';
for (let i = 65; i <= 220; i++) {
str += String.fromCodePoint(i);
}
alert( str );
// ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
//
? . .
a > Z ?
. . a(:97) Z(:90) .
- .
..
.
.
ECMA-402 .
str.localeCompare(str2) ECMA-402 str str2 , , .
strstr2.strstr2.strstr20.
:
alert( 'sterreich'.localeCompare('Zealand') ); // -1
localeCompare . ( ) "a" "a" . .
. , .
.
2 . , , 2 .
2 65,536(2 16 ) . ' (surrogate pair)' 2 .
2.
alert( ''.length ); // 2, X( - )
alert( ''.length ); // 2,
alert( ''.length ); // 2, ()
. .
2 , .
String.fromCodePoint str.codePointAt , . String.fromCharCode str.charCodeAt , fromCodePoint, codePointAt .
.
alert( ''[0] ); //
alert( ''[1] ); //
. .
. 0xd800..0xdbff . 0xdc00..0xdfff . 0xd800..0xdbff 0xdc00..0xdfff .
.
// charCodeAt .
alert( ''.charCodeAt(0).toString(16) ); // d835, 0xd800 0xdbff
alert( ''.charCodeAt(1).toString(16) ); // dcb3, 0xdc00 0xdfff
iterable . .
.
a , . UTF-16 . . .
UTF-16 . .
S ' (\u0307) S .
alert( 'S\u0307' ); // S
. .
S ' (\u0323) S .
:
alert( 'S\u0307\u0323' ); // S
, . .
:
let s1 = 'S\u0307\u0323'; // S, S + +
let s2 = 'S\u0323\u0307'; // S, S + +
alert( `s1: ${s1}, s2: ${s2}` );
alert( s1 == s2 ); // false .
' (unicode normalization)' ' .
alert( "S\u0307\u0323".normalize() == "S\u0323\u0307".normalize() ); // true
S , normalize() . \u1e68 .
alert( "S\u0307\u0323".normalize().length ); // 1
alert( "S\u0307\u0323".normalize() == "\u1e68" ); // true
. UTF-16 ' , UTF-16 .
, Unicode Normalization Forms .
- ,
${}. - UTF-16 .
\n.\u....-
[]. -
slicesubstring. -
toLowerCase,toUpperCase. indexOf.includes/startsWith/endsWith.-
localeCompare. .
.
str.trim()().str.repeat(n)n.- MDN .
<code>,<pre>. 10 plnkr, JSBin, codepen .