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 μ μ κ°μ²΄λ₯Ό μ§μ μ¬μ©νμ¬ μμ±ν μ μμ΅λλ€.
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λ μμλ°μ΄νμ ν°λ°μ΄ν λ¬Έμμ΄μ ꡬλΆνμ§ μμ΅λλ€. λ°λΌμ μμ μ΄μ€μΌμ΄ν λ¬Έμλ μμλ°μ΄νλ ν°λ°μ΄νμμ μκ΄ μμ΄ μλν©λλ€.
κΈ΄ λ¬Έμμ΄ λ¦¬ν°λ΄
μμ±ν μ½λκ° λ§€μ° κΈ΄ λ¬Έμμ΄μ ν¬ν¨ν΄μΌ νλ κ²½μ°, λ μμ΄ λ»μ΄λκ°λ ν μ€μ΄λ νΈμ§κΈ°μ μ¬λμ λ°λΌ μλμΌλ‘ μ€μ λκΈ°λ λμ μ§μ μ¬λ¬ μ€λ‘ λλλ λ΄μ©μλ μν₯μ μ£Όμ§ μκ³ μΆμ λκ° μμκ²λλ€. μ΄λ° μν©μλ λ κ°μ§ λ°©λ²μ μ¬μ©ν μ μμ΅λλ€.
μ°μ λ€μκ³Ό κ°μ΄ + μ°μ°μλ₯Ό μ¬μ©ν μ μμ΅λλ€.
let longString =
"μ¬λ¬ μ€μ κ±Έμ³ μμ±ν΄μΌ ν μ λλ‘ " +
"κΈ΄ λ¬Έμμ΄μΈλ° μ ν μ€μ λ€ μ μΌλ©΄ μλλλ©΄ " +
"μ½λλ₯Ό μ½κΈ° νλ€μ΄μ§λκΉμ.";
μλλ©΄ μμ¬λμ λ¬Έμ("\")λ₯Ό κ° μ€μ 맨 λμ λΆμ¬ λ¬Έμμ΄μ΄ μ΄μ΄μ§λ€λκ±Έ νμν μλ μμ΅λλ€. μμ¬λμ λ€μμ 곡백μ ν¬ν¨ν μ΄λ€ λ¬ΈμλΌλ λΆμΌλ©΄ μ λλ‘ μλνμ§ μμΌλ―λ‘ μ£Όμν΄μΌ ν©λλ€.
let longString =
"μ¬λ¬ μ€μ κ±Έμ³ μμ±ν΄μΌ ν μ λλ‘ \
κΈ΄ λ¬Έμμ΄μΈλ° μ ν μ€μ λ€ μ μΌλ©΄ μλλλ©΄ \
μ½λλ₯Ό μ½κΈ° νλ€μ΄μ§λκΉμ.";
λ μμ λͺ¨λ λμΌν λ¬Έμμ΄μ μμ±ν©λλ€.
μ€λͺ
λ¬Έμμ΄μ ν
μ€νΈ ννλ‘ ννλ μμλ λ°μ΄ν°λ₯Ό 보κ΄νλ λ° μ μ©ν©λλ€. λ¬Έμμ΄μμ κ°μ₯ λ§μ΄ μ¬μ©λλ μμ
λ€μ λ¬Έμμ΄μ κΈΈμ΄λ₯Ό νμΈνλ (length), λ¬Έμμ΄μ μμ±νκ³ μ°κ²°νλ + μ += λ¬Έμμ΄ μ°μ°μ, μλΈλ¬Έμμ΄(substring)μ΄ μλμ§ νμΈνκ³ , μμΌλ©΄ μμΉλ₯Ό νμΈνλ indexOf() λ©μλ, μλΈλ¬Έμμ΄(substring)μ μΆμΆν΄λ΄λ substring() λ©μλκ° μμ΅λλ€.
λ¬Έμ μ κ·Ό
λ¬Έμμ΄μμ κ°κ°μ λ¬Έμμ μ κ·Όν μ μλ λ°©λ²μ λκ°μ§κ° μμ΅λλ€. 첫λ²μ§Έλ charAt() λ©μλλ₯Ό μ΄μ©νλ κ²μ
λλ€:
return "cat".charAt(1); // returns "a"
λ€λ₯Έ λ°©λ²(ECMAScript 5μμ μκ°νκ³ μλ)μ λ¬Έμμ΄μ λ°°μ΄κ³Ό κ°μ μ€λΈμ νΈλ‘ μ·¨κΈνμ¬, λ¬Έμμ ν΄λΉνλ μ«μ μΈλ±μ€λ₯Ό μ¬μ©νλ λ°©λ²μ λλ€ :
return "cat"[1]; // returns "a"
λΈλΌμΌ([ ]) νκΈ°λ²μ μ¬μ©νμ¬ λ¬Έμμ μ κ·Όνλ κ²½μ° , μ΄λ¬ν νλ‘νΌν°λ€μ μλ‘μ΄ κ°μ ν λΉνκ±°λ μμ ν μλ μμ΅λλ€. ν¬ν¨λμ΄ μλ νλ‘νΌν°λ€μ μμ±ν μλ, μμ ν μλ μμ΅λλ€. (λ μμΈν μ 보λ Object.defineProperty()λ₯Ό μ°Έκ³ λ°λλλ€ .)
λ¬Έμμ΄ λΉκ΅
C κ°λ°μλ λ¬Έμμ΄ λΉκ΅λ₯Ό μνμ¬ strcmp() ν¨μλ₯Ό μ¬μ©ν©λλ€. JavaScriptμμλ λ¨μ§ less-thanμ greater-than μ°μ°μλ§μ μ¬μ©νμ¬ λ¬Έμμ΄μ λΉκ΅ν μ μμ΅λλ€:
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λ μλμΌλ‘ λ¬Έμμ΄ μνμ κ°μΈκ³ νλ‘νΌν° μ‘°νλ₯Ό μν νκ±°λ λ©μλλ₯Ό νΈμΆν©λλ€.
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 μ€λΈμ νΈλ€μ λ€λ₯Έ λͺ¨λ μ€λΈμ νΈλ€κ³Ό λ§μ°¬κ°μ§λ‘ μ·¨κΈνλ©°, μ€λΈμ νΈλ₯Ό λ°νν©λλ€. μλ₯Ό λ€λ©΄:
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() λ©μλλ‘ μνμ λμνλλ‘ μ νν μ μμ΅λλ€.
console.log(eval(s2.valueOf())); // returns the number 4
μμ±μ
String()-
Creates a new
Stringobject. 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
lengthof 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
Stringobject of the first occurrence ofsearchValue, or-1if not found. String.prototype.lastIndexOf(searchValue [, fromIndex])-
Returns the index within the calling
Stringobject of the last occurrence ofsearchValue, or-1if not found. String.prototype.localeCompare(compareString [, locales [, options]])-
Returns a number indicating whether the reference string
compareStringcomes before, after, or is equivalent to the given string in sort order. String.prototype.match(regexp)-
Used to match regular expression
regexpagainst 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
counttimes. String.prototype.replace(searchFor, replaceWith)-
Used to replace occurrences of
searchForusingreplaceWith.searchFormay be a string or Regular Expression, andreplaceWithmay be a string or function. String.prototype.replaceAll(searchFor, replaceWith)-
Used to replace all occurrences of
searchForusingreplaceWith.searchFormay be a string or Regular Expression, andreplaceWithmay be a string or function. String.prototype.search(regexp)-
Search for a match between a regular expression
regexpand 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μ λν΄μλ μ λμν©λλ€. μλ₯Ό λ€λ©΄:
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> |