[ Web Proxy ]
URL:
Viewing: https://ko.javascript.info/string [Back]  [Original]

:
Light themeDark theme
DanskEnglishEspaolFranaisIndonesiaItalianoTrkeOzbek
2022 7 28

. .

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.indexOf(substr, pos) .

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)

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])

start end(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, 2 

start end . .

let str = "stringify";

//  4   1 
alert( str.slice(-4, -1) ); // gif
str.substring(start [, end])

start end .

substring slice start end .

:

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])

start length .

substr substring slice .

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 .

.

.

  1. .

    alert( 'a' > 'Z' ); // true
  2. (diacritical mark) .

    alert( 'sterreich' > 'Zealand' ); // true (sterreich     - )

    . sterreich Zealand .

.

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

\u 16 .

// 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 (IE10 Intl.js ).

ECMA-402 .

str.localeCompare(str2) ECMA-402 str str2 , , .

  • str str2 .
  • str str2 .
  • str str2 0 .

:

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)' ' .

str.normalize() .

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... .
  • [] .
  • slice substring .
  • toLowerCase, toUpperCase .
  • indexOf . includes/startsWith/endsWith .
  • localeCompare . .

.

  • str.trim() ().
  • str.repeat(n) n .
  • MDN .

.

: 5

str , ucFirst(str) . .

ucFirst("john") == "John";

.

.

. .

let newStr = str[0].toUpperCase() + str.slice(1);

str str[0] undefined . undefined toUpperCase() .

.

  1. str.charAt(0) str , .
  2. .

.

function ucFirst(str) {
  if (!str) return str;

  return str[0].toUpperCase() + str.slice(1);
}

alert( ucFirst("john") ); // John

.

: 5

str 'viagra 'XXX true checkSpam(str) . false .

.

checkSpam('buy ViAgRA now') == true
checkSpam('free xxxxx') == true
checkSpam("innocent rabbit") == false

.

.

function checkSpam(str) {
  let lowerStr = str.toLowerCase();

  return lowerStr.includes('viagra') || lowerStr.includes('xxx');
}

alert( checkSpam('buy ViAgRA now') );
alert( checkSpam('free xxxxx') );
alert( checkSpam("innocent rabbit") );

.

: 5

str , maxlength str ("") truncate(str, maxlength) . maxlength .

.

:

truncate("What I'd like to tell on this topic is:", 20) = "What I'd like to te"

truncate("Hi everyone!", 20) = "Hi everyone!"

.

maxlength , "" .

. .

function truncate(str, maxlength) {
  return (str.length > maxlength) ?
    str.slice(0, maxlength - 1) + '' : str;
}

.

: 4

"$120" .

extractCurrencyValue(str) .

.

alert( extractCurrencyValue('$120') === 120 ); // true

.

function extractCurrencyValue(str) {
  return +str.slice(1);
}

.

.

Web Proxy Viewer  |  New URL  |  Original Page