String

https://devdocs.io/javascript/global_objects/string

String μ „μ—­κ°μ²΄λŠ” λ¬Έμžμ—΄μ˜ μƒμ„±μžμ΄λ‹€.

Syntax

λ¬Έμžμ—΄ λ¦¬ν„°λŸ΄μ€ πŸ‘‡

'sting text'
"string text"
"μ˜μ–΄ ν•œκ΅­μ–΄ 기타 λ“±λ“± λ‹€μ–‘ν•œ μ–Έμ–΄λ₯Ό 수용"

'', "" λ₯Ό μ‚¬μš©

global objectλ₯Ό λ°”λ‘œ λ§Œλ“€μ–΄μ„œ μ‚¬μš©ν•  수 μžˆλ‹€.

String(thing)

Parameters

thing string μ•„λ¬΄κ°’μ΄λ‚˜.

Template literals

ECMAScript 2015λΆ€ν„°λŠ” string literal이 Template Iiteralsκ°€ string literal이 될 수 μžˆλ‹€.

`hello ${world}`

Escape notation

특수문자 ν‘œν˜„ 이런걸 Escape notation 이라고 ν•˜λŠ”κ΅¬λ‚˜ πŸ€”

javascriptλŠ” μž‘μ€ λ”°μ˜΄ν‘œμ™€ 큰 λ”°μ˜΄ν‘œλ₯Ό κ΅¬λΆ„ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— 상관없이 λ™μž‘ν•œλ‹€.

μ½”λ“œ

좜λ ₯

\XXX

8μ§„μˆ˜ Latin-1 문자 (where XXX is 1–3 octal digits; range of 0–377)

\'

"\'" => "''" μž‘μ€ λ”°μ˜΄ν‘œ

\"

"\"" => """ 큰 λ”°μ˜΄ν‘œ

\\

"\\" => "\" μ—­μŠ¬λž˜μ‹œ

\n

"\n abc" => " abc" κ°œν–‰

\r

carriage return 이것이 무엇인가.. μ˜ˆμ „ νƒ€μžκΈ°λ₯Ό 예둜 λ“€μ–΄μ•Ό ν•  정도 line feed와 carriage return이 ν•¨κ»˜ν•΄μ•Ό λ‹€μŒμ€„ μ‹œμž‘λΆ€λΆ„μ—μ„œ 글을 μ“Έ 수 μžˆμ—ˆλ‹€. https://stackoverflow.com/questions/12747722/what-is-the-difference-between-a-line-feed-and-a-carriage-return μ°Έκ³ 

\v

vertical tab πŸ€”

\b

backspace

\f

form feed If you were programming for a 1980s-style printer, it would eject the paper and start a new page. You are virtually certain to never need it. https://stackoverflow.com/questions/4334370/escape-sequence-f-form-feed-what-exactly-is-it

\uXXXX

unicode codepoint "\u0056" => "V"

\xXX

Latin-1 문자 (where XX is 2 hex digits; range of 0x00–0xFF)

Long literal strings

ν•œμ€„μ„ μ—¬λŸ¬μ€„λ‚΄μ— μž…λ ₯ν•˜κ³  싢을 λ•Œμ—”

+μ—°μ‚°μž ν˜Ήμ€ "\" μ—­μŠ¬λž˜μ‹œλ₯Ό μ΄μš©ν•˜μ—¬ κΈ΄ λ¬Έμžμ—΄μ„ κ°œν–‰ν•˜μ—¬λ„ ν•œμ€„λ‘œ 보여쀄 수 μžˆλ‹€.

let longString1 = "μ—¬λŸ¬ 쀄 μž‘μ„± ν•œλ‹€.. " + 
                                    "μ—¬λŸ¬ 쀄.."
let longString2 = "μ—¬λŸ¬μ€„ μž‘μ„± ν•œλ‹€.. \
                                    μ—­μŠ¬λž˜μ‹œλŠ” μ—­μŠ¬λž˜μ‹œ λ‹€μŒμ— 곡백을 ν¬ν•¨ν•œ \
                  μ–΄λ–€ λ¬Έμžκ°€ μ™€μ„œλŠ” μ•ˆλœλ‹€."

μ—­μŠ¬λž˜μ‹œλ₯Ό μ‚¬μš©ν•  경우 μ—­μŠ¬λž˜μ‹œ λ‹€μŒμ— μ–΄λ–€ λ¬Έμžλ„ μ™€μ„œλŠ” μ•ˆλœλ‹€.

Description

λ¬Έμžμ—΄μ€ ν…μŠ€νŠΈ ν˜•νƒœλ‘œ ν‘œν˜„λ  수 μžˆλŠ” 데이터λ₯Ό λ³΄κ΄€ν•˜λŠ” 데 μœ μš©ν•˜λ‹€. κ°€μž₯ 많이 μ‚¬μš©λ˜λŠ” 것은 length μ—°κ²°ν•˜λŠ” +와 += μ—°μ‚°μž μ„œλΈŒ 문자λ₯Ό ν™•μΈν•˜λŠ” substring, 있으면 μœ„μΉ˜λ₯Ό ν™•μΈν•˜λŠ” indexOf() , μ„œλΈŒ 문자λ₯Ό μΆ”μΆœν•˜λŠ” substring()

Character access(λ¬Έμžμ ‘κ·Ό)

  1. charAt() λ©”μ„œλ“œ μ΄μš©ν•˜κΈ°

    return 'cat'.charAt(1); // returns "a"
  2. ECMAScript 5μ—μ„œ μ†Œκ°œν•˜κ³  μžˆλŠ” λ¬Έμžμ—΄μ„ λ°°μ—΄κ³Ό 같은 object둜 μ·¨κΈ‰ν•˜μ—¬ λ°°μ—΄μ²˜λŸΌ 인덱슀둜 μ ‘κ·Όν•˜κΈ°

    return 'cat'[1]; // returns "a"

    ν•˜μ§€λ§Œ λ°°μ—΄μ²˜λŸΌ μƒˆλ‘œμš΄ 값을 ν• λ‹Ήν•˜κ±°λ‚˜ μ‚­μ œν•  수 μ—†λ‹€.

λ°°μ—΄κ³Ό λ‹€λ₯΄λ‹€

const str = "foo";
const arr = ["f","o","o"];

str.length; // 3
arr.length; // 3

str.indexOf("o"); // 1
arr.indexOf("o"); // 1

const strConcat = str.concat("bar"); // "foobar"
const arrConcat = arr.concat(["b", "a", "r"]); // ["f","o","o,"b","a","r"]

str === strConcat // false
arr === arrConcat // false

str // "foo"
arr // ["f","o","o"]

μ΄λŸ°μ μ„ 보고 λ‘˜ λ‹€ "문자의 λ°°μ—΄"이라고 생각할 수 μžˆμ§€λ§Œ μ•„λ‹ˆλ‹€.

str[1] = "0";
arr[1] = "0";

str; // "foo"
arr; // ["f", "0", "o"]
  1. λ¬Έμžμ—΄μ€ "λΆˆλ³€ κ°’ Immutable"μ΄μ§€λ§Œ 배열은 "κ°€λ³€κ°’ Mutable"이닀.

    str[1]번처럼 νŠΉμ •λ¬Έμžλ₯Ό μ ‘κ·Όν•˜λŠ” ν˜•νƒœκ°€ λͺ¨λ“  μžλ°”μŠ€ν¬λ¦½νŠΈ μ—”μ§„μ—μ„œ μœ νš¨ν•œ 것은 μ•„λ‹ˆλ‹€.

    μ‹€μ œ 인터넷 μ΅μŠ€ν”Œλ‘œλŸ¬ 7은 이λ₯Ό 문법 μ—λŸ¬λ‘œ μΈμ‹ν•œλ‹€.

    str.charAt(1)둜 μ ‘κ·Όν•˜λŠ”κ²Œ λ§žλ‹€.

  2. λ¬Έμžμ—΄μ€ λΆˆλ³€ κ°’μœΌλ‘œ λ¬Έμžμ—΄ λ©”μ„œλ“œλŠ” κ·Έ λ‚΄μš©μ„ λ°”λ‘œ λ³€κ²½ν•˜μ§€ μ•Šκ³  항상 μƒˆλ‘œμš΄ λ¬Έμžμ—΄μ€ μƒμ„±ν•œ ν›„ λ°˜ν™˜ν•œλ‹€. ν•˜μ§€λ§Œ λ°°μ—΄λ©”μ„œλ“œλŠ” κ·Έ μžλ¦¬μ—μ„œ μˆ˜μ •ν•œλ‹€.

λ¬Έμžμ—΄μ„ λ‹€λ₯Ό λ•Œ μœ μš©ν•œ λ°°μ—΄ λ©”μ„œλ“œλŠ” λ¬Έμžμ—΄μ— μ“Έ 수 μ—†μ§€λ§Œ, λ¬Έμžμ—΄μ— λŒ€ν•΄ λΆˆλ³€ λ°°μ—΄ λ©”μ„œλ“œλ₯Ό 빌렀 μ“Έ 수 μžˆλ‹€.

str.join; // undefined
str.map; // undefined

// The join() method creates and returns a new string by concatenating all of the elements in an array
const strJoin = Array.prototype.join.call(str, "-");

// map은 μƒˆλ‘œμš΄ 배열을 returnν•˜κΈ° λ•Œλ¬Έμ— join()ν•˜μ§€ μ•ŠμœΌλ©΄ λ°°μ—΄λ‘œ return
const strMap = Array.prototype.map.call(str, function(v){
  return v.toUpperCase() + ".";
}).join("");

strJoin // "f-o-o"
strMap // "F.O.O."

λ°°μ—΄μ˜ κ°€λ³€ λ©”μ„œλ“œλŠ” μ‚¬μš©ν•  수 μ—†λ‹€.

// The reverse() method reverses an array in place.
Array.prototype.reverse.call(str);
// String 객체 래퍼λ₯Ό λ°˜ν™˜ν•œλ‹€.
str // "foo" :(

λ¬Έμžμ—΄μ€ λΆˆλ³€ 값이라 λ°”λ‘œλ°”λ‘œ λ³€κ²½λ˜μ§€ μ•ŠμœΌλ―€λ‘œ λ°°μ—΄μ˜ κ°€λ³€ λ©”μ„œλ“œλŠ” λ©”μ„œλ“œλŠ” ν†΅ν•˜μ§€ μ•Šκ³ , 빌렀 μ“°λŠ” 것 λ˜ν•œ μ•ˆλœλ‹€.

λ¬Έμžμ—΄μ„ λ°°μ—΄λ‘œ λ°”κΎΈκ³  μ›ν•˜λŠ” μž‘μ—…μ„ μˆ˜ν–‰ν•œ ν›„ λ‹€μ‹œ λ¬Έμžμ—΄λ‘œ λ°”κΎΈλŠ” Hack을 μ‚¬μš©ν•˜κΈ°λ„ ν•œλ‹€. 이 μ‚¬μš©μ€ Codewarsμ—μ„œ 맀우 흔흔.. ν•˜μ§€λ§Œ μ΄λŸ¬ν•œ 방법은 μœ λ‹ˆμ½”λ“œκ°€ μ„žμ—¬ μžˆλŠ” 경우(특수문자, Multibyte λ“±) 이 방법이 ν†΅ν•˜μ§€ μ•ŠλŠ”λ‹€.

생각을 λ‹¬λ¦¬ν•˜μ—¬ μž‘μ—…μ΄ 빈번히 ν•„μš”ν•œ λ¬Έμžμ—΄μ΄λΌλ©΄ 문자 λ‹¨μœ„λ‘œ μ €μž₯ν•˜λŠ” λ°°μ—΄λ‘œ μ·¨κΈ‰ν•˜λŠ”κ²Œ 더 λ‚˜μ„ 수 μžˆλ‹€.

Comparing strings(λ¬Έμžμ—΄ 비ꡐ)

less-thanκ³Ό greater-than μ—°μ‚°μžλ§Œμ„ μ‚¬μš©ν•˜μ—¬ λ¬Έμžμ—΄μ„ 비ꡐ할 수 μžˆλ‹€.

const a = "a";
const b = "b";

if(a === b) console.log(`${a} and ${b} are equal.`);
if(a > b) console.log(`${a} is gerater than ${b}`);
if(a < b) console.log(`${a} is less than ${b}`);

String μΈμŠ€ν„΄νŠΈμ— μƒμ†λœ localeCompare() λ©”μ„œλ“œλ₯Ό μ‚¬μš© ν•  수 μžˆλ‹€.

native

νŠΉμ • λΈŒλΌμš°μ €μ— μ’…μ†λ˜μ§€ μ•Šμ€ ECMAScript λͺ…μ„Έμ˜ λ‚΄μž₯객체λ₯Ό λ§ν•œλ‹€. (Window, Button 등은 μ œμ™Έ)

Stirng()을 ν¬ν•¨ν•˜μ—¬ Number(), Boolean(), Array(), Object(), Function(), RegExp(), Dat(), Error(), Symbol() κ°€μž₯ 많이 μ“°λŠ” λ„€μ΄ν‹°λΈŒ λ„€μ΄ν‹°λΈŒλŠ” λ‚΄μž₯ ν•¨μˆ˜μ΄λ‹€.

native prototype

λ‚΄μž₯ λ„€μ΄ν‹°λΈŒ μƒμ„±μžλŠ” 각자의 .prototype 객체λ₯Ό κ°€μ§„λ‹€.(Array.prototype, String.prototype)

prototype κ°μ²΄μ—λŠ” ν•΄λ‹Ή 객체의 ν•˜μœ„ νƒ€μž…λ³„λ‘œ κ³ μœ ν•œ 둜직이 담겨 μžˆλ‹€.

λ¬Έμžμ—΄ μ›μ‹œ 값을 λ°•μ‹±μœΌλ‘œ ν™•μž₯ν•œ κ²ƒκΉŒμ§€ ν¬ν•¨ν•˜μ—¬ λͺ¨λ“  String κ°μ²΄λŠ” 기본적으둜 Stirng.prototype 객체에 μ •μ˜λœ λ©”μ„œλ“œμ— μ ‘κ·Ό ν•  수 μžˆλ‹€. ν”„λ‘œν† νƒ€μž… μœ„μž„(prototype Delegation) 덕뢄에 λͺ¨λ“  λ¬Έμžμ—΄μ΄ λ©”μ„œλ“œλ“€μ„ 같이 μ“Έ 수 μžˆλ‹€.

String은 "λΆˆλ³€ κ°’ Immutable"이기 λ•Œλ¬Έμ— λ¬Έμžμ—΄ 값을 λ³€κ²½ν•˜λŠ” λ©”μ„œλ“œλŠ” μ—†λ‹€. μˆ˜μ •μ΄ μΌμ–΄λ‚˜λ©΄ μƒˆλ‘œμš΄ 값을 μƒμ„±ν•œλ‹€.

ν”„λ‘œν† νƒ€μž…μ„ λ³€κ²½ν•  μˆ˜λ„ μžˆμ§€λ§Œ, λ³€κ²½ν•˜μ§€ μ•ŠλŠ”κ²Œ μ’‹λ‹€.

Distinction between string primitives and String objects

string primitives와 string objectλŠ” μ„œλ‘œ λ‹€λ₯΄κ²Œ μ·¨κΈ‰ν•œλ‹€.

primitive strings

λ¬Έμžμ—΄ λ¦¬ν„°λŸ΄(μž‘μ€ λ”°μ˜΄ν‘œλ‚˜ 큰 λ”°μ˜΄ν‘œ)κ³Ό μƒμ„±μž 없이(new ν‚€μ›Œλ“œ 없이) String 을 ν˜ΈμΆœν•˜μ—¬ λ°˜ν™˜ν•œ λ¬Έμžμ—΄

const stringPrimitive = "string primitve";
const stringObject = new String('string object');

typeof stringPrimitive // "string"
typeof stringObject // "object"

stringPrimitive instanceof String // false
stringObject instanceof String // true

// The toString() 은 λ¬Έμžμ—΄μ„ λ°˜ν™˜ν•˜λŠ” object의 λŒ€ν‘œμ μΈ 방법
// 이 λ©”μ„œλ“œκ°€ μ‚¬μš©μž μ§€μ • κ°œμ²΄μ—μ„œ μž¬μ •μ˜λ˜μ§€ μ•ŠμœΌλ©΄ toString()은 "[object type]"을 λ°˜ν™˜ν•©λ‹ˆλ‹€. μ—¬κΈ°μ„œ type은 object typeμž…λ‹ˆλ‹€. 
Object.prototype.toString.call(stringPrimitive) // "[object String]"
Object.prototype.toString.call(stringObject) // "[object String]"

new String('string object') μƒμ„±μžμ˜ κ²°κ³ΌλŠ” μ›μ‹œκ°’ 'string object'λ₯Ό 감싼 object wrapper 이닀.

typeof stringObject // "object" 이 κ²°κ³Ό 값을 보면 μžμ‹ μ„ 감싼 μ›μ‹œκ°’μ˜ νƒ€μž…μ΄ μ•„λ‹ˆλΌ object의 ν•˜μœ„ νƒ€μž…μ— 가깝닀.

String {"string object"}
0: "s"
1: "t"
2: "r"
3: "i"
4: "n"
5: "g"
6: " "
7: "o"
8: "b"
9: "j"
10: "e"
11: "c"
12: "t"
length: 13
__proto__: String
[[PrimitiveValue]]: "string object"

console.log둜 ν™•μΈν–ˆμ„ λ•Œ λͺ¨μŠ΅ (λΈŒλΌμš°μ € λ§ˆλ‹€ λ‹€λ₯΄λ‹€)

String, Number, Boolean 같은 λ‹¨μˆœ primitiveλŠ” boxing 고정을 κ±°μΉœλ‹€.

Object.prototype.toString.call(stringPrimitive) // "[object String]"

String 으둜 ν‘œμ‹œλœ 것을 보아 ν•΄λ‹Ή 객체 래퍼둜 μžλ™ 박싱됨을 μ•Œ 수 μžˆλ‹€.

Wrapper Boxing

μ›μ‹œκ°’μ—” ν”„λ‘œνΌν‹°λ‚˜ λ©”μ„œλ“œκ°€ μ—†μœΌλ―€λ‘œ μ ‘κ·Όν•˜κΈ° μœ„ν•΄μ„œλŠ” μ›μ‹œ 값을 객체 레퍼둜 κ°μ‹Έμ•Όν•˜λŠ”λ° -> 이λ₯Ό μžλ°”μŠ€ν¬λ¦½νŠΈκ°€ μžλ™μ μœΌλ‘œ ν•΄μ€€λ‹€.

κ·Έλ ‡λ‹€λ©΄?

const str = "string";
for(let i = 0; i < str.length; i++) {
  console.log(str.charAt(i));
}

str.lengthλŠ” 객체둜 κ°μ‹ΈλŠ” 일이 μƒκΈ°λ‹ˆκΉŒ μ²˜μŒλΆ€ν„° 객체λ₯Ό 생성할 수 없도둝 String Object둜 μ“°λŠ”κ²Œ 더 μ’‹μ„κΉŒ?

닡은 놉!

  1. λΈŒλΌμš°μ €κ°€ 슀슀둜 μ΅œμ ν™” ν•œλ‹€.

  2. κ°œλ°œμžκ°€ μ„  μ΅œμ ν™”(pre-Opimize)ν•˜κ²Œ λœλ‹€λ©΄ 였히렀 더 느렀질 수 μžˆλ‹€.

직접 객체 ν˜•νƒœλ‘œ 써야할 μ΄μœ λŠ” 거의 μ—†λ‹€. μ•Œμ•„λ³΄κΈ° μ‰½κ²Œ μ›μ‹œ 값을 μ‚¬μš©ν•˜μž

μˆ˜λ™μœΌλ‘œ μ›μ‹œ 값을 λ°•μ‹±ν•˜κ³ μž ν•œλ‹€λ©΄ Object()ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜μž

const stringPrimitive = "string primitve";
const stringObject = new String('string object');
const objSting = Object("string");

typeof stringPrimitive // "string"
typeof stringObject // "object"
typeof objSting // "object"

stringPrimitive instanceof String // false
stringObject instanceof String // true
objSting instanceof String // true

Object.prototype.toString.call(stringObject) // "[object String]"
Object.prototype.toString.call(objSting) // "[object String]"

객체 래퍼둜 직접 λ°•μ‹±ν•˜λŠ”κ±΄ κΆŒν•˜μ§€ μ•ŠλŠ”λ‹€.

ν•˜μ§€λ§Œ λ§Œμ•½ new Boolean(false 같은 경우 객체 래퍼둜 truthy 값이 λœλ‹€. μ΄λŸ°κ²½μš°λ“±μ„ λŒ€λΉ„ν•˜μ—¬ Object()둜 ν•˜μŸˆ

μ°Έκ³ 

Last updated

Was this helpful?