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} |
ì ëì½ë ì½ëí¬ì¸í¸ |
\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)-
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® 2027 Language Specification> # sec-string-objects> |