JavaScript . , .
UTF-16, .
JavaScript .
, :
let single = 'single-quoted';
let double = "double-quoted";
let backticks = `backticks`;
, , , , , ${}:
function sum(a, b) {
return a + b;
}
alert(`1 + 2 = ${sum(1, 2)}.`); // 1 + 2 = 3.
, :
let guestList = `Guests:
* John
* Pete
* Mary
`;
alert(guestList); // ,
, ? ? , :
let guestList = "Guests: // Error: Unexpected token ILLEGAL
* John";
: . , , .
. : func`string`. func . . , . , .
, , \n:
let guestList = "Guests:\n * John\n * Pete\n * Mary";
alert(guestList); // ,
, , -:
//
let str1 = "Hello\nWorld";
// ,
let str2 = `Hello
World`;
alert(str1 == str2); // true
, . :
\n |
|
\r |
Windows \r\n, \n. , Windows \n. |
\',\",\` |
|
\\ |
|
\t |
|
\b, \f, \v |
Backspace, Form Feed Vertical Tab , . |
, , \ .
, .
:
alert( 'I\'m the Walrus!' ); // I'm the Walrus!
\' .
, , , . , :
alert( `I'm the Walrus!` ); // I'm the Walrus!
, \ , . , \. alert .
, \?
, !
alert( `The backslash: \\` ); // The backslash: \
length :
alert( `My\n`.length ); // 3
, \n , : 3.
length , , : str.length() str.length. .
str.length , , .
, pos, : [pos]. str.at(pos). :
let str = `Hello`;
//
alert( str[0] ); // H
alert( str.at(0) ); // H
//
alert( str[str.length - 1] ); // o
alert( str.at(-1) ); // o
, .at(pos) , . pos , .
, .at(-1) , .at(-2) , , ..
undefined . :
let str = `Hello`;
alert( str[-2] ); // undefined
alert( str.at(-2) ); // l
, for..of:
for (let char of "Hello") {
alert(char); // H,e,l,l,o (char "H", "e", "l" ..)
}
JavaScript . . .
, , :
let str = 'Hi';
str[0] = 'h'; //
alert( str[0] ); //
.
:
let str = 'Hi';
str = 'h' + str[1]; //
alert( str ); // hi
.
alert( 'Interface'.toUpperCase() ); // INTERFACE
alert( 'Interface'.toLowerCase() ); // interface
- :
alert( 'Interface'[0].toLowerCase() ); // 'i'
.
str.indexOf
substr str, pos, , , -1 .
:
let str = 'Widget with id';
alert( str.indexOf('Widget') ); // 0, 'Widget'
alert( str.indexOf('widget') ); // -1, ,
alert( str.indexOf("id") ); // 1, "id" 1 (..idget with id)
.
, "id" 1. , , 2:
let str = 'Widget with id';
alert( str.indexOf('id', 2) ) // 12
, indexOf . , , :
let str = ' - ';
let target = ''; //
let pos = 0;
while (true) {
let foundPos = str.indexOf(target, pos);
if (foundPos == -1) break;
alert( ` : ${foundPos}` );
pos = foundPos + 1; //
}
:
let str = " - ";
let target = "";
let pos = -1;
while ((pos = str.indexOf(target, pos + 1)) != -1) {
alert( pos );
}
str.lastIndexOf(substr, position)str.lastIndexOf(substr, position), .
, : () .
indexOf if . :
let str = "Widget with id";
if (str.indexOf("Widget")) {
alert(" "); //
}
"Widget", , 0. alert , . . str.indexOf("Widget") 0, if , .
-1:
let str = "Widget with id";
if (str.indexOf("Widget") != -1) {
alert(" "); //
}
~. 32- (signed 32-bit integer). , , , . .
: 32- ~n -(n+1).
:
alert( ~2 ); // -3, , -(2+1)
alert( ~1 ); // -2, , -(1+1)
alert( ~0 ); // -1, , -(0+1)
alert( ~-1 ); // 0, , -(-1+1)
, ~n 0 n == -1 ( n, 32- ).
, if ( ~str.indexOf("") ) , indexOf -1, .
, indexOf :
let str = "Widget";
if (~str.indexOf("Widget")) {
alert( ' ' ); //
}
- , , .
: if (~str.indexOf()) .
, , - , 32 ~, , 0, ~4294967295=0. .
, : .includes (. ).
includes, startsWith, endsWith
str.includes(substr, pos) true, str substr, false, .
, , , :
alert( "Widget with id".includes("Widget") ); // true
alert( "Hello".includes("Bye") ); // false
str.includes :
alert( "Midget".includes("id") ); // true
alert( "Midget".includes("id", 3) ); // false, 3
str.startsWith str.endsWith , , :
alert( "Widget".startsWith("Wid") ); // true, "Wid" "Widget"
alert( "Widget".endsWith("get") ); // true, "get" "Widget"
JavaScript 3 : substring, substr slice.
str.slice(start [, end])-
start( )end.:
let str = "stringify"; // 'strin', 0 5 ( 5) alert( str.slice(0, 5) ); // 's', 0 1, 1, . . 0 alert( str.slice(0, 1) );end,slice:let str = "stringify"; alert( str.slice(2) ); // ringify, 2start/end. , :let str = "stringify"; // 4 , 1 alert( str.slice(-4, -1) ); // gif str.substring(start [, end])-
startend( )end.,
slice,startend.
startend,substring, .:
let str = "stringify"; // substring 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,slice, ,0. str.substr(start [, length])-
startlength., :
let str = "stringify"; // ring, 4 , 2 alert( str.substr(2, 4) );, :
let str = "stringify"; // gi, 2 , 4 alert( str.substr(-4, 2) );Annex B . , JavaScript, . .
, , :
slice(start, end) |
start end ( end) |
|
substring(start, end) |
start end ( end) |
0 |
substr(start, length) |
length , start |
start |
. substr : JavaScript, Annex B. , . , , , . .
, slice , , . , , .
, .
-
:
alert( 'a' > 'Z' ); // true -
, , :
alert( 'sterreich' > 'Zealand' ); // true: ,
Zealandsterreich.
, , JavaScript.
UTF-16. , . , .
str.codePointAt(pos)-
,
pos:// // alert( "z".codePointAt(0) ); // 122 alert( "Z".codePointAt(0) ); // 90 String.fromCodePoint(code)-
codealert( String.fromCodePoint(90) ); // 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).
- , .
- ,
, . ,az.
, , .
, .
.
str.localeCompare(str2) , , :
- ,
strstr2. - ,
strstr2. 0, .
:
alert( 'sterreich'.localeCompare('Zealand') ); // -1
- . , ,
${}. - JavaScript UTF-16.
- ,
\n. -
[]at. -
slicesubstring. - , ,
toLowerCase/toUpperCase. -
indexOfincludes/startsWith/endsWith, , . - ,
localeCompare.
- :
, , , . , .
<code>, —<pre>, 10 — (plnkr, JSBin, codepen)