Baseline Widely available *
String
å
¨åç©ä»¶çºåä¸²çæ§é 彿¸ï¼ææ¯ä¸åå符åºåã
å串æåæ¡ç¨ä¸åå½¢å¼ï¼
'string text' "string text" "䏿 español deutsch English हिनà¥à¤¦à¥ Ø§ÙØ¹Ø±Ø¨ÙØ© português বাà¦à¦²à¦¾ ÑÑÑÑкий æ¥æ¬èª ਪੰà¨à¨¾à¨¬à© íêµì´ தமிழ௠ע×ר×ת"
å串ä¹å¯ä»¥è¢«å
¨åç String
ç©ä»¶å»ºç«ï¼
String(thing)忏
thing
ä»»ä½è¦è½ææå串çç©ä»¶ã
èª ECMAScript 2015ï¼å串æåä¹å¯ä»¥æ¯æ¨£æ¿åé¢å¼ï¼Template literalsï¼ï¼
`hello world` `hello! world!` `hello ${who}` escape `<a>${who}</a>`è·³è«ç¬¦è
é¤äºå¸¸è¦çãå¯å°åºä¾çåå ï¼ç¹æ®åå ä¹å¯ä»¥è¢«è·³è«ç¬¦èä¾è¡¨ç¤ºç·¨ç¢¼ã
代碼 輸åº\0
空åå
\'
å®å¼è \"
éå¼è \\
åæç· \n
æ·è¡ \r
åè» \v
åç´å¶è¡¨ \t
å¶è¡¨ \b
éæ ¼ \f
é¥é \uXXXX
unicode 代碼 \u{X}
... \u{XXXXXX}
unicode 代碼 坦驿§è³ª \xXX
Latin-1 åå
åè¨»ï¼ åå ¶ä»èªè¨ä¸åï¼JavaScript å°å®å¼èå串åéå¼èå串æ¯åç¸åï¼å æ¤ï¼ä¸è¿°çåºåå¯ä»¥å¨å®å¼èæéå¼èä¸ä½ç¨ã
é·åé¢å¼å串æäºæåï¼ä½ çç¨å¼ç¢¼æå å«é常é·çå串ã çºäºä¸è®é·åä¸²ç¡æ¢ç¡å°å¾ä¸é·ï¼æææ¯å¨ä½ å¿è¡ä¾æ½®çæåï¼ä½ å¯è½å¸æå°é樣é·çå串è½å¤ æ·æå¤è¡å»ä¸å½±é¿å°å¯¦éçå §å®¹ã
ä½ å¯ä»¥ç¨ + éç®åéå å¤åå串å¨ä¸èµ·ï¼åæ¯é樣ï¼
let longString =
"This is a very long string which needs " +
"to wrap across multiple lines because " +
"otherwise my code is unreadable.";
æè ï¼ä½ å¯ä»¥å¨æ¯ä¸è¡å°¾ç«¯ç¨åæç·åå ï¼"\"ï¼è¡¨ç¤ºå串æç¹¼çºè¢«é¡¯ç¤ºå¨ä¸ä¸åã ä½ å¿ é è¦ç¢ºå®å¨åæç·å¾é¢æ²æä»»ä½ç©ºç½æå ¶ä»åå ï¼çè³æ¯ç¸®æï¼å¦åéåæ¹æ³å°å¤±æã éåå½¢å¼çèµ·ä¾å鿍£ï¼
let longString =
"This is a very long string which needs \
to wrap across multiple lines because \
otherwise my code is unreadable.";
éå ©åæ¹æ³é½æå»ºç«ç¸åçåä¸²å §å®¹ã
說æå䏲尿¼è½ä¿ç以æåå½¢å¼è¡¨éçè³æéä»¶äºä¾èªªï¼æ¯æç¨çãå¨å串ä¸ï¼ä¸äºæå¸¸è¢«ä½¿ç¨çéç®å³ç¢ºèªå串é·åº¦ length
ï¼ç¨ + æ += å串éç®å
å»ºé æè
串æ¥å串ï¼ç¨ indexOf
æ¹æ³æª¢æ¥?åå串æ¯å¦å卿åå串çä½ç½®ï¼æè
æ¯ç¨ substring
æ¹æ³å°åå串æ½ååºä¾ã
æå
©åæ¹æ³å¯ä»¥ååå串ä¸åå¥çåå
ã第ä¸åæ¯ç¨ charAt
æ¹æ³ï¼
return "cat".charAt(1); // åå³ "a"
å¦ä¸å(å¨ ECMAScript 5 ä¸è¢«æå°)æ¹æ³æ¯å°å串ç¶ä½ä¸åé¡ä¼¼é£åçç©ä»¶ï¼ç´æ¥ååå串ä¸å°æçæ¸å¼ç´¢å¼ã
return "cat"[1]; // åå³ "a"
å°æ¼åååå
使ç¨çæ¬è表éå¼ï¼æ²è¾¦æ³å»åªé¤æææ´¾ä¸åå¼çµ¦éäºå±¬æ§ã éäºå±¬æ§æ¢éå¯å¯«çï¼ä¹éå¯è¨å®çã(åè¦ Object.defineProperty
)
C èªè¨çéç¼è
æ strcmp()
å½å¼å¯ä»¥ç¨ä¾æ¯è¼å串ã å¨ JavaScript ä¸ï¼ä½ åªè½ç¨å°æ¼å大æ¼éç®åï¼
var a = "a";
var b = "b";
if (a < b)
// true
print(a + " å°æ¼ " + b);
else if (a > b) print(a + " å¤§æ¼ " + b);
else print(a + " å " + b + " ç¸ç");
鿍£é¡ä¼¼ççµæï¼ä¹è½ä½¿ç¨ç¹¼æ¿ String
實é«ç localeCompare
æ¹æ³ä¾å¯¦ç¾ã
String
ç©ä»¶
è«æ³¨æï¼JavaScript æåå¥ String
ç©ä»¶ååå§å串(Boolean
å Numbers
乿¯å¦æ¤)ã
String literals (denoted by double or single quotes) and strings returned from String
calls in a non-constructor context (i.e., without using the new
keyword) are primitive strings. JavaScript automatically converts primitives to String
objects, so that it's possible to use String
object methods for primitive strings. In contexts where a method is to be invoked on a primitive string or a property lookup occurs, JavaScript will automatically wrap the string primitive and call the method or perform the property lookup.
var s_prim = "foo";
var s_obj = new String(s_prim);
console.log(typeof s_prim); // å°åº "string"
console.log(typeof s_obj); // å°åº "object"
å串åå§åå¥å String
ç©ä»¶ä¹æå¨ä½¿ç¨ eval
æçµ¦åºä¸åççµæã åå§åå¥å³é² eval
æè¢«è¦çºåå§ä»£ç¢¼ï¼String
ç©ä»¶åæåå³ï¼ä¸è¢«è¦ä½æ¯å
¶ä»ç©ä»¶ãèåä¾åï¼
s1 = "2 + 2"; // 建ç«ä¸åå串åå§åå¥
s2 = new String("2 + 2"); // 建ç«ä¸åå串ç©ä»¶
console.log(eval(s1)); // å峿¸å 4
console.log(eval(s2)); // åå³å串 "2 + 2"
å çºä¸äºåå ï¼ç¨å¼ç¢¼ä¹è¨±å¨éå° String
ç©ä»¶æï¼ä½éè¦ç廿¯å串åå§åå¥ï¼åç®¡å¦æ¤ï¼é常ä½è
åä¸éè¦æå¿å®çå·®ç°ã
ä¸å String
ç©ä»¶ç¸½è½å¤ ä½¿ç¨ valueOf
æ¹æ³è¢«è½ææèªèº«çåå§å¯æ¬ã
console.log(eval(s2.valueOf())); // å峿¸å 4
屬æ§
String.prototype
è½è®å串ç©ä»¶å¢å 屬æ§ã
String.fromCharCode()
å©ç¨ Unicode å¼çåºå建ç«ä¸¦åå³ä¸åå串ã
String.fromCodePoint()
坦驿§è³ª
å©ç¨ç·¨ç¢¼ä½ç½®çåºå建ç«ä¸¦åå³ä¸åå串ã
String
éç¨æ¹æ³
è¦åï¼ å串éç¨æ¹æ³æ¯éæ¨æºåçã被æ£ç¨çï¼ä¹æè¿æå°è¢«åªé¤çã
The String
instance methods are also available in Firefox as of JavaScript 1.6 (though not part of the ECMAScript standard) on the String object for applying String methods to any object:
var num = 15;
alert(String.replace(num, /5/, "2"));
Generics are also available on Array
methods.
The following is a shim to provide support to non-supporting browsers:
/*globals define*/
// Assumes all supplied String instance methods already present
// (one may use shims for these if not available)
(function () {
"use strict";
var i,
// We could also build the array of methods with the following, but the
// getOwnPropertyNames() method is non-shimable:
// Object.getOwnPropertyNames(String).filter(function (methodName)
// {return typeof String[methodName] === 'function'});
methods = [
"quote",
"substring",
"toLowerCase",
"toUpperCase",
"charAt",
"charCodeAt",
"indexOf",
"lastIndexOf",
"startsWith",
"endsWith",
"trim",
"trimLeft",
"trimRight",
"toLocaleLowerCase",
"toLocaleUpperCase",
"localeCompare",
"match",
"search",
"replace",
"split",
"substr",
"concat",
"slice",
],
methodCount = methods.length,
assignStringGeneric = function (methodName) {
var method = String.prototype[methodName];
String[methodName] = function (arg1) {
return method.apply(arg1, Array.prototype.slice.call(arguments, 1));
};
};
for (i = 0; i < methodCount; i++) {
assignStringGeneric(methods[i]);
}
})();
String
instances Properties
String.prototype.length
Reflects the length
of the string. Read-only.
String.prototype.at()
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()
Returns the character (exactly one UTF-16 code unit) at the specified index
.
String.prototype.charCodeAt()
Returns a number that is the UTF-16 code unit value at the given index
.
String.prototype.codePointAt()
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()
Combines the text of two (or more) strings and returns a new string.
String.prototype.includes()
Determines whether the calling string contains searchString
.
String.prototype.endsWith()
Determines whether a string ends with the characters of the string searchString
.
String.prototype.indexOf()
Returns the index within the calling String
object of the first occurrence of searchValue
, or -1
if not found.
String.prototype.lastIndexOf()
Returns the index within the calling String
object of the last occurrence of searchValue
, or -1
if not found.
String.prototype.localeCompare()
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()
Used to match regular expression regexp
against a string.
String.prototype.matchAll()
Returns an iterator of all regexp
's matches.
String.prototype.normalize()
Returns the Unicode Normalization Form of the calling string value.
String.prototype.padEnd()
Pads the current string from the end with a given string and returns a new string of the length targetLength
.
String.prototype.padStart()
Pads the current string from the start with a given string and returns a new string of the length targetLength
.
String.prototype.repeat()
Returns a string consisting of the elements of the object repeated count
times.
String.prototype.replace()
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()
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()
Search for a match between a regular expression regexp
and the calling string.
String.prototype.slice()
Extracts a section of a string and returns a new string.
String.prototype.split()
Returns an array of strings populated by splitting the calling string at occurrences of the substring sep
.
String.prototype.startsWith()
Determines whether the calling string begins with the characters of string searchString
.
String.prototype.substring()
Returns a new string containing characters of the calling string from (or between) the specified index (or indices).
String.prototype.toLocaleLowerCase()
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.
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[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.
It's possible to use String
as a "safer" toString
alternative, as although it still normally calls the underlying toString
, it also works for null
and undefined
. For example:
var outputStrings = [];
for (let i = 0, n = inputValues.length; i < n; ++i) {
outputStrings.push(String(inputValues[i]));
}
è¦ç¯ ç覽å¨ç¸å®¹æ§ åè¦
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4