| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String | [Back] [Original] |
Get to know MDN better
const string1 = "A string primitive";
const string2 = 'Also a string primitive';
const string3 = `Yet another string primitive`;
const string4 = new String("A String object");
"cat".charAt(1); // gives value "a"
"cat"[1]; // gives value "a"
writableconfigurable Object.defineProperty()
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} `);
}
function areEqualCaseInsensitive(str1, str2) {
return str1.toUpperCase() === str2.toUpperCase();
}
toUpperCase() toLowerCase() ss toUpperCase() SS toLowerCase() I toLocaleLowerCase("tr")
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"
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
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
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()
console.log(eval(s2.valueOf())); // 4
undefined "undefined"null "null"true "true"false "false"toString(10) toString(10) BigIntTypeError[Symbol.toPrimitive]()hint "string"toString() valueOf() JavaScript
`${x}` String(x) symbol "" + x
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
"".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.prototype 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()String.prototype.toLocaleUpperCase( [locale, ...locales])String.prototype.toLowerCase()String.prototype.toString()String.prototype.toUpperCase()String.prototype.toWellFormed()String.prototype.trim()String.prototype.trimEnd()String.prototype.trimStart()String.prototype.valueOf()String.prototype[Symbol.iterator]()String String
HTML HTML innerHTML DOM API document.createElement()
String.prototype.anchor() String.prototype.big() <big>String.prototype.blink() <blink>
String.prototype.bold() <b>String.prototype.fixed() <tt>String.prototype.fontcolor() String.prototype.fontsize() String.prototype.italics() <i>String.prototype.link() <a href="url"> URL
String.prototype.small() <small>String.prototype.strike() <strike>String.prototype.sub() <sub>String.prototype.sup() <sup>HTML HTML
"</b>".bold(); // <b></b></b>
" anchor()fontcolor()fontsize() link() "
"foo".anchor('"Hello"'); // <a name=""Hello"">foo</a>
String toString() null undefined
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 |
StringString.prototype.anchor()String.prototype.at()String.prototype.big()String.prototype.blink()String.prototype.bold()String.prototype.charAt()String.prototype.charCodeAt()String.prototype.codePointAt()String.prototype.concat()String.prototype.endsWith()String.prototype.fixed()String.prototype.fontcolor()String.prototype.fontsize()String.prototype.includes()String.prototype.indexOf()String.prototype.isWellFormed()String.prototype.italics()String.prototype.lastIndexOf()String.prototype.link()String.prototype.localeCompare()String.prototype.match()String.prototype.matchAll()String.prototype.normalize()String.prototype.padEnd()String.prototype.padStart()String.prototype.repeat()String.prototype.replace()String.prototype.replaceAll()String.prototype.search()String.prototype.slice()String.prototype.small()String.prototype.split()String.prototype.startsWith()String.prototype.strike()String.prototype.sub()String.prototype.substr()String.prototype.substring()String.prototype.sup()String.prototype.toLocaleLowerCase()String.prototype.toLocaleUpperCase()String.prototype.toLowerCase()String.prototype.toString()String.prototype.toUpperCase()String.prototype.toWellFormed()String.prototype.trim()String.prototype.trimEnd()String.prototype.trimStart()String.prototype.valueOf()String.prototype[Symbol.iterator]()Object/FunctionObject.prototype.__defineGetter__()Object.prototype.__defineSetter__()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toLocaleString()Object.prototype.toString()Object.prototype.valueOf()| Web Proxy Viewer | New URL | Original Page |