pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/

μ„±μžμž…λ‹ˆλ‹€." />

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

String

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015λ…„ 7μ›”.

* Some parts of this feature may have varying levels of support.

String μ „μ—­ κ°μ²΄λŠ” λ¬Έμžμ—΄(문자의 λ‚˜μ—΄)의 μƒμ„±μžμž…λ‹ˆλ‹€.

ꡬ문

λ¬Έμžμ—΄ λ¦¬ν„°λŸ΄μ€ λ‹€μŒκ³Ό 같은 ν˜•μ‹μ„ μ‚¬μš©ν•©λ‹ˆλ‹€.

'string text'
"string text"
"δΈ­ζ–‡ espaΓ±ol Deutsch English ΰ€¦ΰ₯‡ΰ€΅ΰ€¨ΰ€Ύΰ€—ΰ€°ΰ₯€ Ψ§Ω„ΨΉΨ±Ψ¨ΩŠΨ© portuguΓͺs বাংলা русский ζ—₯本θͺž norsk bokmΓ₯l ΰ¨ͺΰ©°ΰ¨œΰ¨Ύΰ¨¬ΰ©€ ν•œκ΅­μ–΄ ΰ€ΰΰΏΰ΄ΰ― Χ’Χ‘Χ¨Χ™Χͺ"

λ¬Έμžμ—΄μ€ String μ „μ—­ 객체λ₯Ό 직접 μ‚¬μš©ν•˜μ—¬ 생성할 수 μžˆμŠ΅λ‹ˆλ‹€.

js
String(thing);

λ§€κ°œλ³€μˆ˜

thing
λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•  아무 κ°’.

ν…œν”Œλ¦Ώ λ¦¬ν„°λŸ΄

ECMAScript 2015 이후, λ¬Έμžμ—΄ λ¦¬ν„°λŸ΄μ€ μ†Œμœ„ ν…œν”Œλ¦Ώ λ¦¬ν„°λŸ΄μ΄ 될 수 μžˆμŠ΅λ‹ˆλ‹€.

`hello world` `hello! world!` `hello ${who}` tag `<a>${who}</a>`

μ΄μŠ€μΌ€μ΄ν”„ ν‘œν˜„

일반적인 좜λ ₯ 문자 μ™Έμ˜ 특수 λ¬ΈμžλŠ” μ΄μŠ€μΌ€μ΄ν”„ ν‘œν˜„μ„ μ‚¬μš©ν•΄ 적을 수 μžˆμŠ΅λ‹ˆλ‹€.

μ½”λ“œ 좜λ ₯
\XXX 8μ§„μˆ˜ Latin-1 문자
\' μž‘μ€λ”°μ˜΄ν‘œ
\" ν°λ”°μ˜΄ν‘œ
\\ μ—­μŠ¬λž˜μ‹œ
\n κ°œν–‰
\r 캐리지 리턴
\v μ„Έλ‘œ νƒ­
\t νƒ­
\b 백 슀페이슀
\f 폼 ν”Όλ“œ
\uXXXX μœ λ‹ˆμ½”λ“œ μ½”λ“œν¬μΈνŠΈ
\u{X} ... \u{XXXXXX} μœ λ‹ˆμ½”λ“œ μ½”λ“œν¬μΈνŠΈ Experimental
\xXX Latin-1 문자

μ°Έκ³  : 일뢀 λ‹€λ₯Έ ν”„λ‘œκ·Έλž˜λ° 언어와 달리, JavaScriptλŠ” μž‘μ€λ”°μ˜΄ν‘œμ™€ ν°λ”°μ˜΄ν‘œ λ¬Έμžμ—΄μ„ κ΅¬λΆ„ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. λ”°λΌμ„œ μœ„μ˜ μ΄μŠ€μΌ€μ΄ν”„ λ¬ΈμžλŠ” μž‘μ€λ”°μ˜΄ν‘œλ‚˜ ν°λ”°μ˜΄ν‘œμ—μ„œ 상관 없이 μž‘λ™ν•©λ‹ˆλ‹€.

κΈ΄ λ¬Έμžμ—΄ λ¦¬ν„°λŸ΄

μž‘μ„±ν•œ μ½”λ“œκ°€ 맀우 κΈ΄ λ¬Έμžμ—΄μ„ 포함해야 ν•˜λŠ” 경우, 끝 없이 λ»—μ–΄λ‚˜κ°€λŠ” ν•œ μ€„μ΄λ‚˜ νŽΈμ§‘κΈ°μ˜ μž¬λŸ‰μ— 따라 μžλ™μœΌλ‘œ 쀄을 λ„˜κΈ°λŠ” λŒ€μ‹  직접 μ—¬λŸ¬ μ€„λ‘œ λ‚˜λˆ„λ˜ λ‚΄μš©μ—λŠ” 영ν–₯을 μ£Όμ§€ μ•Šκ³  싢을 λ•Œκ°€ μžˆμ„κ²λ‹ˆλ‹€. 이런 μƒν™©μ—λŠ” 두 κ°€μ§€ 방법을 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

μš°μ„  λ‹€μŒκ³Ό 같이 + μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

js
let longString =
  "μ—¬λŸ¬ 쀄에 걸쳐 μž‘μ„±ν•΄μ•Ό ν•  μ •λ„λ‘œ " +
  "κΈ΄ λ¬Έμžμ—΄μΈλ° μ™œ ν•œ 쀄에 λ‹€ 적으면 μ•ˆλ˜λƒλ©΄ " +
  "μ½”λ“œλ₯Ό 읽기 νž˜λ“€μ–΄μ§€λ‹ˆκΉŒμš”.";

μ•„λ‹ˆλ©΄ μ—­μŠ¬λž˜μ‹œ 문자("\")λ₯Ό 각 μ€„μ˜ 맨 끝에 λΆ™μ—¬ λ¬Έμžμ—΄μ΄ μ΄μ–΄μ§„λ‹€λŠ”κ±Έ ν‘œμ‹œν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€. μ—­μŠ¬λž˜μ‹œ λ‹€μŒμ— 곡백을 ν¬ν•¨ν•œ μ–΄λ–€ λ¬ΈμžλΌλ„ λΆ™μœΌλ©΄ μ œλŒ€λ‘œ μž‘λ™ν•˜μ§€ μ•ŠμœΌλ―€λ‘œ μ£Όμ˜ν•΄μ•Ό ν•©λ‹ˆλ‹€.

js
let longString =
  "μ—¬λŸ¬ 쀄에 걸쳐 μž‘μ„±ν•΄μ•Ό ν•  μ •λ„λ‘œ \
κΈ΄ λ¬Έμžμ—΄μΈλ° μ™œ ν•œ 쀄에 λ‹€ 적으면 μ•ˆλ˜λƒλ©΄ \
μ½”λ“œλ₯Ό 읽기 νž˜λ“€μ–΄μ§€λ‹ˆκΉŒμš”.";

두 μ˜ˆμ‹œ λͺ¨λ‘ λ™μΌν•œ λ¬Έμžμ—΄μ„ μƒμ„±ν•©λ‹ˆλ‹€.

μ„€λͺ…

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

문자 μ ‘κ·Ό

λ¬Έμžμ—΄μ—μ„œ 개개의 λ¬Έμžμ— μ ‘κ·Όν•  수 μžˆλŠ” 방법은 두가지가 μžˆμŠ΅λ‹ˆλ‹€. μ²«λ²ˆμ§ΈλŠ” charAt() λ©”μ„œλ“œλ₯Ό μ΄μš©ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€:

js
return "cat".charAt(1); // returns "a"

λ‹€λ₯Έ 방법(ECMAScript 5μ—μ„œ μ†Œκ°œν•˜κ³  μžˆλŠ”)은 λ¬Έμžμ—΄μ„ λ°°μ—΄κ³Ό 같은 였브젝트둜 μ·¨κΈ‰ν•˜μ—¬, λ¬Έμžμ— ν•΄λ‹Ήν•˜λŠ” 숫자 인덱슀λ₯Ό μ‚¬μš©ν•˜λŠ” λ°©λ²•μž…λ‹ˆλ‹€ :

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

λΈŒλΌμΌ“([ ]) ν‘œκΈ°λ²•μ„ μ‚¬μš©ν•˜μ—¬ λ¬Έμžμ— μ ‘κ·Όν•˜λŠ” 경우 , μ΄λŸ¬ν•œ ν”„λ‘œνΌν‹°λ“€μ— μƒˆλ‘œμš΄ 값을 ν• λ‹Ήν•˜κ±°λ‚˜ μ‚­μ œν•  μˆ˜λŠ” μ—†μŠ΅λ‹ˆλ‹€. ν¬ν•¨λ˜μ–΄ μžˆλŠ” ν”„λ‘œνΌν‹°λ“€μ€ μž‘μ„±ν•  μˆ˜λ„, μˆ˜μ •ν•  μˆ˜λ„ μ—†μŠ΅λ‹ˆλ‹€. (더 μžμ„Έν•œ μ •λ³΄λŠ” Object.defineProperty()λ₯Ό μ°Έκ³  λ°”λžλ‹ˆλ‹€ .)

λ¬Έμžμ—΄ 비ꡐ

C κ°œλ°œμžλŠ” λ¬Έμžμ—΄ 비ꡐλ₯Ό μœ„ν•˜μ—¬ strcmp() ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€. JavaScriptμ—μ„œλŠ” 단지 less-than와 greater-than μ—°μ‚°μžλ§Œμ„ μ‚¬μš©ν•˜μ—¬ λ¬Έμžμ—΄μ„ 비ꡐ할 수 μžˆμŠ΅λ‹ˆλ‹€:

js
var a = "a";
var b = "b";
if (a < b) {
  // true
  console.log(a + " is less than " + b);
} else if (a > b) {
  console.log(a + " is greater than " + b);
} else {
  console.log(a + " and " + b + " are equal.");
}

λΉ„μŠ·ν•œ κ²°κ³Όλ₯Ό 얻을 수 μžˆλŠ” λ°©λ²•μœΌλ‘œ String μΈμŠ€ν„΄μŠ€μ—μ„œ μƒμ†λœ localeCompare() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

λ¬Έμžμ—΄ μ›ν˜•κ³Ό String 객체의 차이

JavaScriptλŠ” String μ˜€λΈŒμ νŠΈμ™€ μ›ν˜•μ˜ λ¬Έμžμ—΄μ„ λ‹€λ₯΄κ²Œ μ·¨κΈ‰ν•œλ‹€λŠ” 것에 μ£Όμ˜ν•΄μ•Ό ν•©λ‹ˆλ‹€. (Booleanκ³Ό 숫자의 true도 λ§ˆμ°¬κ°€μ§€μž…λ‹ˆλ‹€.)

λ¬Έμžμ—΄ λ¦¬ν„°λŸ΄(μž‘μ€ λ”°μ˜΄ν‘œ λ˜λŠ” 큰 λ”°μ˜΄ν‘œλ‘œ μƒμ„±λ˜λŠ”)κ³Ό μƒμ„±μž 없이(즉. new ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šκ³ ) String을 ν˜ΈμΆœν•˜μ—¬ λ°˜ν™˜λœ λ¬Έμžμ—΄μ€ μ›ν˜• λ¬Έμžμ—΄(primitive strings)μž…λ‹ˆλ‹€. JavaScriptλŠ” μžλ™μ μœΌλ‘œ μ›ν˜•μ„ String 였브젝트둜 λ³€ν™˜ν•˜κΈ° λ•Œλ¬Έμ—, String 였브젝트 λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ μ›ν˜•λ¬Έμžμ—΄μ„ 생성할 수 μžˆμŠ΅λ‹ˆλ‹€. λ¬Έλ§₯ μ•ˆμ˜ λ©”μ„œλ“œμ—μ„œ ν”„λ‘œνΌν‹° 쑰회 λ˜λŠ” μ›ν˜•μ˜ λ¬Έμžμ—΄ 호좜이 λ°œμƒν•˜λ©΄, JavaScriptλŠ” μžλ™μœΌλ‘œ λ¬Έμžμ—΄ μ›ν˜•μ„ 감싸고 ν”„λ‘œνΌν‹° 쑰회λ₯Ό μˆ˜ν–‰ ν•˜κ±°λ‚˜ λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•©λ‹ˆλ‹€.

js
var s_prim = "foo";
var s_obj = new String(s_prim);

console.log(typeof s_prim); // Logs "string"
console.log(typeof s_obj); // Logs "object"

λ¬Έμžμ—΄ μ›ν˜•κ³Ό String μ˜€λΈŒμ νŠΈλŠ” eval()을 μ‚¬μš©ν•  λ•Œ λ‹€λ₯Έ κ²°κ³Όλ₯Ό μ œκ³΅ν•©λ‹ˆλ‹€. eval에 μ „λ‹¬λ˜λŠ” λ¬Έμžμ—΄ μ›ν˜•λ“€μ€ μ†ŒμŠ€ μ½”λ“œ 취급을 λ°›μŠ΅λ‹ˆλ‹€; String μ˜€λΈŒμ νŠΈλ“€μ€ λ‹€λ₯Έ λͺ¨λ“  μ˜€λΈŒμ νŠΈλ“€κ³Ό λ§ˆμ°¬κ°€μ§€λ‘œ μ·¨κΈ‰ν•˜λ©°, 였브젝트λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€. 예λ₯Ό λ“€λ©΄:

js
var s1 = "2 + 2"; // creates a string primitive
var s2 = new String("2 + 2"); // creates a String object
console.log(eval(s1)); // returns the number 4
console.log(eval(s2)); // returns the string "2 + 2"

μ΄λŸ¬ν•œ 이유둜, 비둝 μ½”λ“œ μƒμ—μ„œ μ›ν˜• λ¬Έμžμ—΄μ„ μ‚¬μš©ν•˜λŠ” λŒ€μ‹ μ— String 였브젝트λ₯Ό μ‚¬μš©ν•˜κ²Œ 되면 μ½”λ“œκ°€ 손상될 수 μžˆμ§€λ§Œ, 일반적인 κ°œλ°œμžλŠ” 차이λ₯Ό κ±±μ •ν•  ν•„μš”λŠ” μ—†μŠ΅λ‹ˆλ‹€.

String μ˜€λΈŒμ νŠΈλŠ” μ–Έμ œλ“ μ§€ valueOf() λ©”μ„œλ“œλ‘œ μ›ν˜•μ— λŒ€μ‘ν•˜λ„λ‘ μ „ν™˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

js
console.log(eval(s2.valueOf())); // returns the number 4

μƒμ„±μž

String()

Creates a new String object. It performs type conversion when called as a function, rather than as a constructor, which is usually more useful.

정적 λ©”μ„œλ“œ

String.fromCharCode(num1 [, ...[, numN]])

μ§€μ •λœ μœ λ‹ˆμ½”λ“œ κ°’μ˜ μˆœμ„œλ₯Ό μ΄μš©ν•˜μ—¬ λ§Œλ“  λ¬Έμžμ—΄μ„ λ°˜ν™˜ν•©λ‹ˆλ‹€.

String.fromCodePoint(num1 [, ...[, numN)

μ§€μ •λœ μ½”λ“œ 포인트 μˆœμ„œλ₯Ό μ΄μš©ν•˜μ—¬ λ§Œλ“  λ¬Έμžμ—΄μ„ λ°˜ν™˜ν•©λ‹ˆλ‹€.

String.raw()

μ›ν˜• ν…œν”Œλ¦Ώ λ¬Έμžμ—΄(raw template string)μ—μ„œ μƒμ„±λœ λ¬Έμžμ—΄μ„ λ°˜ν™˜ν•©λ‹ˆλ‹€.

μΈμŠ€ν„΄μŠ€ 속성

String.prototype.length

Reflects the length of the string. Read-only.

μΈμŠ€ν„΄μŠ€ λ©”μ„œλ“œ

String.prototype.at(index) Experimental

Returns the character (exactly one UTF-16 code unit) at the specified index. Accepts negative integers, which count back from the last string character.

String.prototype.charAt(index)

Returns the character (exactly one UTF-16 code unit) at the specified index.

String.prototype.charCodeAt(index)

Returns a number that is the UTF-16 code unit value at the given index.

String.prototype.codePointAt(pos)

Returns a nonnegative integer Number that is the code point value of the UTF-16 encoded code point starting at the specified pos.

String.prototype.concat(str [, ...strN ])

Combines the text of two (or more) strings and returns a new string.

String.prototype.includes(searchString [, position])

Determines whether the calling string contains searchString.

String.prototype.endsWith(searchString [, length])

Determines whether a string ends with the characters of the string searchString.

String.prototype.indexOf(searchValue [, fromIndex])

Returns the index within the calling String object of the first occurrence of searchValue, or -1 if not found.

String.prototype.lastIndexOf(searchValue [, fromIndex])

Returns the index within the calling String object of the last occurrence of searchValue, or -1 if not found.

String.prototype.localeCompare(compareString [, locales [, options]])

Returns a number indicating whether the reference string compareString comes before, after, or is equivalent to the given string in sort order.

String.prototype.match(regexp)

Used to match regular expression regexp against a string.

String.prototype.matchAll(regexp)

Returns an iterator of all regexp's matches.

String.prototype.normalize([form])

Returns the Unicode Normalization Form of the calling string value.

String.prototype.padEnd(targetLength [, padString])

Pads the current string from the end with a given string and returns a new string of the length targetLength.

String.prototype.padStart(targetLength [, padString])

Pads the current string from the start with a given string and returns a new string of the length targetLength.

String.prototype.repeat(count)

Returns a string consisting of the elements of the object repeated count times.

String.prototype.replace(searchFor, replaceWith)

Used to replace occurrences of searchFor using replaceWith. searchFor may be a string or Regular Expression, and replaceWith may be a string or function.

String.prototype.replaceAll(searchFor, replaceWith)

Used to replace all occurrences of searchFor using replaceWith. searchFor may be a string or Regular Expression, and replaceWith may be a string or function.

String.prototype.search(regexp)

Search for a match between a regular expression regexp and the calling string.

String.prototype.slice(beginIndex[, endIndex])

Extracts a section of a string and returns a new string.

String.prototype.split([sep [, limit] ])

Returns an array of strings populated by splitting the calling string at occurrences of the substring sep.

String.prototype.startsWith(searchString [, length])

Determines whether the calling string begins with the characters of string searchString.

String.prototype.substring(indexStart [, indexEnd])

Returns a new string containing characters of the calling string from (or between) the specified index (or indices).

String.prototype.toLocaleLowerCase( [locale, ...locales])

The characters within a string are converted to lowercase while respecting the current locale.For most languages, this will return the same as toLowerCase().

String.prototype.toLocaleUpperCase( [locale, ...locales])

The characters within a string are converted to uppercase while respecting the current locale.For most languages, this will return the same as toUpperCase().

String.prototype.toLowerCase()

Returns the calling string value converted to lowercase.

String.prototype.toString()

Returns a string representing the specified object. Overrides the Object.prototype.toString() method.

String.prototype.toUpperCase()

Returns the calling string value converted to uppercase.

String.prototype.trim()

Trims whitespace from the beginning and end of the string. Part of the ECMAScript 5 standard.

String.prototype.trimStart()

Trims whitespace from the beginning of the string.

String.prototype.trimEnd()

Trims whitespace from the end of the string.

String.prototype.valueOf()

Returns the primitive value of the specified object. Overrides the Object.prototype.valueOf() method.

String.prototype.@@iterator()

Returns a new iterator object that iterates over the code points of a String value, returning each code point as a String value.

예제

λ¬Έμžμ—΄ λ³€ν™˜

비둝 일반적으둜 toString() ν•¨μˆ˜λ₯Ό 많이 μ‚¬μš©ν•˜κ³  μžˆμ§€λ§Œ, toString()의 "μ•ˆμ „ν•œ" λŒ€μ•ˆμœΌλ‘œ String을 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. String은 nullκ³Ό undefined에 λŒ€ν•΄μ„œλ„ 잘 λ™μž‘ν•©λ‹ˆλ‹€. 예λ₯Ό λ“€λ©΄:

js
var outputStrings = [];
for (var i = 0, n = inputValues.length; i < n; ++i) {
  outputStrings.push(String(inputValues[i]));
}

λͺ…μ„Έμ„œ

Specification
ECMAScriptΒ 2026 LanguageΒ Specification
# sec-string-objects

λΈŒλΌμš°μ € ν˜Έν™˜μ„±

같이 보기

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy