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

String - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

String

*

20157

*

String

+ += indexOf() substring()

String()

js
const string1 = "A string primitive";
const string2 = 'Also a string primitive';
const string3 = `Yet another string primitive`;
js
const string4 = new String("A String object");

`

charAt()

js
"cat".charAt(1); // gives value "a"

js
"cat"[1]; // gives value "a"

writableconfigurable Object.defineProperty()

js
const a = "a";
const b = "b";
if (a < b) {
  // true
  console.log(`${a}  ${b}`);
} else if (a > b) {
  console.log(`${a}  ${b}`);
} else {
  console.log(`${a}  ${b} `);
}

=== ==

js
function areEqualCaseInsensitive(str1, str2) {
  return str1.toUpperCase() === str2.toUpperCase();
}

toUpperCase() toLowerCase() ss toUpperCase() SS toLowerCase() I toLocaleLowerCase("tr")

js
const areEqualInUpperCase = (str1, str2) =>
  str1.toUpperCase() === str2.toUpperCase();
const areEqualInLowerCase = (str1, str2) =>
  str1.toLowerCase() === str2.toLowerCase();

areEqualInUpperCase("", "ss"); // true false
areEqualInLowerCase("", "I"); // false true

locale-aware Intl.Collator API localeCompare() sensitivity "accent" "base"

js
const areEqual = (str1, str2, locale = "en-US") =>
  str1.localeCompare(str2, locale, { sensitivity: "accent" }) === 0;

areEqual("", "ss", "de"); // false
areEqual("", "I", "tr"); // true

localeCompare() strcmp()

JavaScript String Boolean Number

String new JavaScript

js
const strPrim = "foo"; // 
const strPrim2 = String(1); // 1
const strPrim3 = String(true); // true
const strObj = new String(strPrim); //  `new`  `String` 

console.log(typeof strPrim); // "string"
console.log(typeof strPrim2); // "string"
console.log(typeof strPrim3); // "string"
console.log(typeof strObj); // "object"

String

eval() String eval String

js
const s1 = "2 + 2"; // 
const s2 = new String("2 + 2"); //  String 
console.log(eval(s1)); //  4
console.log(eval(s2)); // 2 + 2

String

String valueOf()

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

String

JavaScript

`${x}` String(x) symbol "" + x

UTF-16 Unicode

UTF-16 UTF-16 16 216 65536 UTF-16 BMP \u 4

Unicode 65536 surrogate pair UTF-16 16 0xD800 0xDFFF 0xD800 0xDBFF 0xDC00 0xDFFF Unicode UTF-16 Unicode code point Unicode \u{xxxxxx} xxxxxx 16

lone surrogate 16

  • 0xD800 0xDBFF
  • 0xDC00 0xDFFF

Unicode JavaScript UTF-16 encodeURI() URIError URI UTF-8 UTF-8 UTF-16 encodeURI() TextEncoder isWellFormed() toWellFormed()

Unicode Unicode grapheme cluster emoji emoji emoji <ZWJ>U+200D

split("") UTF-16 UTF-16 [Symbol.iterator]() Unicode

js
"".split(""); // ['\ud83d', '\ude04']; splits into two lone surrogates

// "Backhand Index Pointing Right: Dark Skin Tone"
[...""]; // ['', '']
// splits into the basic "Backhand Index Pointing Right" emoji and
// the "Dark skin tone" emoji

// "Family: Man, Boy"
[...""]; // [ '', '', '' ]
// splits into the "Man" and "Boy" emoji, joined by a ZWJ

// The United Nations flag
[...""]; // [ '', '' ]
// splits into two "region indicator" letters "U" and "N".
// All flag emojis are formed by joining two region indicator letters

String()

String

String.fromCharCode()

Unicode

String.fromCodePoint()

String.raw()

String.prototype String

String.prototype.constructor

String String

String

String.prototype.length

length

String.prototype.at()

UTF-16

String.prototype.charAt()

index UTF-16

String.prototype.charCodeAt()

index UTF-16

String.prototype.codePointAt()

pos UTF-16

String.prototype.concat()

String.prototype.endsWith()

searchString

String.prototype.includes()

searchString

String.prototype.indexOf()

String searchValue -1

String.prototype.isWellFormed()

String.prototype.lastIndexOf()

String searchValue -1

String.prototype.localeCompare()

compareString

String.prototype.match()

regexp

String.prototype.matchAll()

regexp

String.prototype.normalize()

Unicode

String.prototype.padEnd()

targetLength

String.prototype.padStart()

targetLength

String.prototype.repeat()

count

String.prototype.replace()

replaceWith searchForsearchFor replaceWith

String.prototype.replaceAll()

replaceWith searchForsearchFor replaceWith

String.prototype.search()

regexp

String.prototype.slice()

String.prototype.split()

sep

String.prototype.startsWith()

searchString

String.prototype.substring()

String.prototype.toLocaleLowerCase()

toLowerCase()

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

toUpperCase()

String.prototype.toLowerCase()

String.prototype.toString()

Object.prototype.toString()

String.prototype.toUpperCase()

String.prototype.toWellFormed()

Unicode U+FFFD

String.prototype.trim()

String.prototype.trimEnd()

String.prototype.trimStart()

String.prototype.valueOf()

Object.prototype.valueOf()

String.prototype[Symbol.iterator]()

String String

HTML

String.prototype.anchor()

<a name="name">

String.prototype.big()
<big>

<blink>

String.prototype.bold()
<b>
String.prototype.fixed()
<tt>
String.prototype.fontcolor()

<font color="color">

String.prototype.fontsize()

<font size="size">

String.prototype.italics()
<i>

<a href="url"> URL

String.prototype.small()
<small>
String.prototype.strike()
<strike>
String.prototype.sub()
<sub>
String.prototype.sup()
<sup>

HTML HTML

js
"</b>".bold(); // <b></b></b>

" anchor()fontcolor()fontsize() link() &quot;

js
"foo".anchor('"Hello"'); // <a name="&quot;Hello&quot;">foo</a>

String toString() null undefined

js
const nullVar = null;
nullVar.toString(); // TypeError: nullVar is null
String(nullVar); // "null"

const undefinedVar = undefined;
undefinedVar.toString(); // TypeError: undefinedVar is undefined
String(undefinedVar); // "undefined"

ECMAScript 2027 LanguageSpecification
# sec-string-objects


Web Proxy Viewer  |  New URL  |  Original Page