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(๋ฌธ์์ ๊ทผ)
charAt() ๋ฉ์๋ ์ด์ฉํ๊ธฐ
return 'cat'.charAt(1); // returns "a"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"]๋ฌธ์์ด์ "๋ถ๋ณ ๊ฐ Immutable"์ด์ง๋ง ๋ฐฐ์ด์ "๊ฐ๋ณ๊ฐ Mutable"์ด๋ค.
str[1]๋ฒ์ฒ๋ผ ํน์ ๋ฌธ์๋ฅผ ์ ๊ทผํ๋ ํํ๊ฐ ๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ ์์ง์์ ์ ํจํ ๊ฒ์ ์๋๋ค.
์ค์ ์ธํฐ๋ท ์ต์คํ๋ก๋ฌ 7์ ์ด๋ฅผ ๋ฌธ๋ฒ ์๋ฌ๋ก ์ธ์ํ๋ค.
str.charAt(1)๋ก ์ ๊ทผํ๋๊ฒ ๋ง๋ค.
๋ฌธ์์ด์ ๋ถ๋ณ ๊ฐ์ผ๋ก ๋ฌธ์์ด ๋ฉ์๋๋ ๊ทธ ๋ด์ฉ์ ๋ฐ๋ก ๋ณ๊ฒฝํ์ง ์๊ณ ํญ์ ์๋ก์ด ๋ฌธ์์ด์ ์์ฑํ ํ ๋ฐํํ๋ค. ํ์ง๋ง ๋ฐฐ์ด๋ฉ์๋๋ ๊ทธ ์๋ฆฌ์์ ์์ ํ๋ค.
๋ฌธ์์ด์ ๋ค๋ฅผ ๋ ์ ์ฉํ ๋ฐฐ์ด ๋ฉ์๋๋ ๋ฌธ์์ด์ ์ธ ์ ์์ง๋ง, ๋ฌธ์์ด์ ๋ํด ๋ถ๋ณ ๋ฐฐ์ด ๋ฉ์๋๋ฅผ ๋น๋ ค ์ธ ์ ์๋ค.
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๋ก ์ฐ๋๊ฒ ๋ ์ข์๊น?
๋ต์ ๋!
๋ธ๋ผ์ฐ์ ๊ฐ ์ค์ค๋ก ์ต์ ํ ํ๋ค.
๊ฐ๋ฐ์๊ฐ ์ ์ต์ ํ(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()๋ก ํ์
์ฐธ๊ณ
You Don't Know JS: ํ์ ๊ณผ ๋ฌธ๋ฒ, ์ค์ฝํ์ ํด๋ก์
https://maximdenisov.gitbooks.io/you-don-t-know-js/content/types_&_grammar/natives.html
Last updated
Was this helpful?