Hướng dẫn string operators in javascript
The Strings are useful for holding data that can be represented in text form. Some of the most-used
operations on strings are to check their Strings can be created as primitives, from string literals, or as objects, using the
String primitives and string objects share many behaviors, but have other important differences and caveats. See "String primitives and String objects" below. String literals can be specified using single or double quotes, which are treated identically, or using the backtick character `. This last form specifies a template literal: with this form you can interpolate expressions. Character access There are two ways to
access an individual character in a string. The first is the
The other way is to treat the string as an array-like object, where individual characters correspond to a numerical index:
When using bracket notation for character access, attempting to delete or assign a value to these properties will not
succeed. The properties involved are neither writable nor configurable. (See Comparing strings In C, the
Note that all comparison operators,
including
The choice of whether to transform by
A locale-aware and robust solution for testing case-insensitive equality is to use the
The String primitives and String objects Note that JavaScript distinguishes between String literals (denoted by double or single quotes) and strings returned from
Warning: You should rarely find yourself using String primitives and
For these reasons, the code may break when it encounters A
String coercionMany built-in operations that expect strings would first coerce their arguments to strings (which is largely why
There are several ways to achieve nearly the same effect in JavaScript.
Depending on your use case, you may want to use Escape sequencesSpecial characters can be encoded using escape sequences: Long literal stringsSometimes, your code will include strings which are very long. Rather than having lines that go on endlessly, or wrap at the whim of your editor, you may wish to specifically break the string into multiple lines in the source code without affecting the actual string contents. You
can use the
Or you can use the backslash character (
Both of the above methods result in identical strings. UTF-16 characters, Unicode codepoints, and grapheme clustersStrings are represented fundamentally as sequences of UTF-16 code units. In UTF-16
encoding, every code unit is exact 16 bits long. This means there are a maximum of 216, or 65536 possible characters representable as single UTF-16 code units. This character set is called the basic multilingual plane (BMP), and includes the most common characters like the Latin, Greek, Cyrillic alphabets, as well as many East Asian characters. Each code unit can be
written in a string with However, the entire Unicode character set is much, much bigger than 65536. The extra characters are stored in UTF-16 as surrogate pairs, which are pairs of 16-bit code units that represent a single character. To avoid ambiguity, the two parts of the pair must be between On top of Unicode characters, there are certain sequences of Unicode characters that should be treated as one visual unit, known as a grapheme cluster. The most common case is emojis: many emojis that have a range of variations are actually formed by multiple emojis, usually joined by the You must be careful which level of characters you are iterating on. For example,
Constructor
Creates a new Static methods
Returns a string created by using the specified sequence of Unicode values. String.fromCodePoint() Returns a string created by using the specified sequence of code points. String.raw() Returns a string created from a raw template string. Instance properties
Reflects the Instance methods
Returns the character (exactly one UTF-16 code unit) at the specified String.prototype.charAt() Returns the character (exactly one UTF-16 code unit) at the specified String.prototype.charCodeAt() Returns a
number that is the UTF-16 code unit value at the given 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 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 String.prototype.endsWith() Determines whether a string ends with the characters of the string String.prototype.indexOf() Returns the index within the calling
String.prototype.lastIndexOf() Returns the index within the calling
String.prototype.localeCompare() Returns a number indicating whether the reference string String.prototype.match() Used to match regular expression String.prototype.matchAll() Returns
an iterator of all 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 String.prototype.padStart() Pads the current string from the start with a given string and returns a new string of the length String.prototype.repeat() Returns a string consisting of the elements of the object repeated String.prototype.replace() Used to replace occurrences of String.prototype.replaceAll() Used to replace all occurrences of String.prototype.search() Search for a match between a regular expression 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 String.prototype.startsWith() Determines whether
the calling string begins with the characters of string 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 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
String.prototype.toLowerCase() Returns the calling string value converted to lowercase. String.prototype.toString() Returns a string representing the specified object. Overrides the 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 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. HTML wrapper methodsWarning: Deprecated. Avoid these methods. They are of limited use, as they are based on a very old HTML standard and provide only a subset of the currently available HTML tags and attributes. Many of them create deprecated or non-standard markup
today. In addition, they do simple string concatenation without any validation or sanitation, which makes them a potential security threat when directly inserted using String.prototype.anchor() DeprecatedString.prototype.big() Deprecated
String.prototype.blink() Deprecated
String.prototype.bold() Deprecated
String.prototype.fixed()
Deprecated
String.prototype.fontcolor() Deprecated
String.prototype.fontsize() Deprecated
String.prototype.italics() Deprecated
String.prototype.link() DeprecatedString.prototype.small() Deprecated
String.prototype.strike()
Deprecated
String.prototype.sub() Deprecated
String.prototype.sup() Deprecated
Note that these methods do not check if the string itself contains HTML tags, so it's possible to create invalid HTML:
The only escaping they do is to replace
ExamplesString conversion It's possible to use
Specifications
Browser compatibilityBCD tables only load in the browser See also |