Published 2022. 11. 10. 11:35

String Length

length속성

let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let length = text.length;

 

 

Escape Character

Code Result Description
\' ' Single quote
\" " Double quote
\\ \ Backslash

 

 

Extracting String Parts

  • slice(start, end)
  • substring(start, end)
  • substr(start, length)

 

slice()

let text = "Apple, Banana, Kiwi";
let part = text.slice(7, 13);
Banana

 

 

 

Replacing String Content

let text = "Please visit Microsoft!";
let newText = text.replace("Microsoft", "jcy");

기본적으로 이 replace()메서드는 첫 번째 일치 항목만 대체합니다.

 

대소문자를 구분하지 않으려면 다음과 같이 플래그가 있는 정규식을 사용합니다(비구분)

/i
let text = "Please visit Microsoft!";
let newText = text.replace(/MICROSOFT/i, "jcy");

 

 

모든 일치 항목을 바꾸려면 정규식  /g플래그(전역 일치)

/g
let text = "Please visit Microsoft and Microsoft!";
let newText = text.replace(/Microsoft/g, "jcy");

 

 

 

ReplaceAll()

text = text.replaceAll("Cats","Dogs");
text = text.replaceAll("cats","dogs");

 

 

 

toUpperCase()

let text1 = "Hello World!";
let text2 = text1.toUpperCase();

 

toLowerCase()

let text1 = "Hello World!";       // String
let text2 = text1.toLowerCase();  // text2 is text1 converted to lower

 

 

concat()

let text1 = "Hello";
let text2 = "World";
let text3 = text1.concat(" ", text2);
text = "Hello" + " " + "World!";
text = "Hello".concat(" ", "World!");

 

 

 

trim()

let text1 = "      Hello World!      ";
let text2 = text1.trim();

 

 

 

trimStart()

let text1 = "     Hello World!     ";
let text2 = text1.trimStart();

 

 

 

trimEnd()

let text1 = "     Hello World!     ";
let text2 = text1.trimEnd();

 

 

 

 

padStart()

let text = "5";
let padded = text.padStart(4,"x");
xxx5

 

 

 

Extracting String Characters

  • charAt(position)
  • charCodeAt(position)
  • 속성 액세스 [ ]

 

charAt()

let text = "HELLO WORLD";
let char = text.charAt(0);

 

 

charCodeAt()

let text = "HELLO WORLD";
let char = text.charCodeAt(0);
72

 

 

속성 액세스 [ ]

let text = "HELLO WORLD";
let char = text[0];
H
let text = "HELLO WORLD";
text[0] = "A";    // Gives no error, but does not work
HELLO WORLD

 

 

 

split()

text.split(",")    // Split on commas
text.split(" ")    // Split on spaces
text.split("|")    // Split on pipe
text.split("")

 

'JavaScript' 카테고리의 다른 글

JavaScript - Template Literals  (0) 2022.11.10
JavaScript - String Search  (0) 2022.11.10
JavaScript - Object  (0) 2022.11.10
JavaScript - Output  (0) 2022.11.08
Javascript - document.getElementById()  (0) 2022.09.22
복사했습니다!