[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String [Back]  [Original]

String - JavaScript | MDN

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. Its been available across browsers since 2015 7.

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

String ( ) .

In this article

.

'string text'
"string text"
" espaol Deutsch English   portugus    norsk bokml    "

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}
\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)

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/Symbol.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 2027 LanguageSpecification
# sec-string-objects


Web Proxy Viewer  |  New URL  |  Original Page