String definitions:
1,var mystring= "content"; or var mystring= ' content '
2,var mystring= New String ("content") creates an object---, but adds overhead
Content:
1, adding single quotation marks (double quotes) in double quotation marks (single quotes) does not need to be escaped, if double quotation marks (single quotation marks) are required in double quotation mark (single quotation mark)
2, the string by default can only be defined by a row of non-wrapped, such as need to be divided into multiple lines, you can use the \ before line-breaking settings; (older browsers do not support)
3, common escape sequences:
\ t |
Tabs |
\ n |
Wrap, insert a line break in the character character |
\" |
Double quotes |
\‘ |
Single quotation marks |
\\ |
Reverse Slash |
\x99 |
The value of the ASCII character, expressed as a 2-bit 16 binary |
\u9999 |
The value of the uniformly encoded character, expressed as a 4-bit 16 binary |
4, character and array (same point)
The string can be manipulated like an array:
You can also manipulate strings directly:
If the value inside [] is not a number, or is out of range, or uses a letter:
The string is similar to an array, and the string is an array-like object that cannot change a single character within a string. The string also cannot add a new property.
A single character inside a string cannot be changed or deleted. The Length property, which is calculated from 1, returns the lengths of the strings that cannot be changed. But no error.
Analytical:
Inside JavaScript, the variable s actually points to the address of the string "Hello World", and "Hello World" itself is a constant, so it cannot be changed, neither new nor deleted.
On the other hand, when a string is called, it is automatically converted to an instance of the string object, and the object is automatically destroyed when the call ends. This means that the next time the property of the string is called, it is actually called a temporary generated new object, not the one generated on the last call , so the property assigned to the previous object is not taken. If you want to add a property to a string, it is only defined on its prototype object String.prototype
5,BASE64 encoding
- Btoa (): string or binary value converted to BASE64 encoding
- Atob (): BASE64 Encoding to original encoding
Character passing:
To pass a character to a function, you cannot use double quotation marks, but instead use single quotation marks to pass in, which is difficult to manipulate.
You can pass a number to a function without quotation marks, but you must enclose the character in quotation marks, or an undefined error will occur.
Character Connections:
Use + to combine characters with string1+string2.
String object:
An instance of a string object is an array-like object.
String can also be used as a tool method to convert any type of value to a string.
String.fromCharCode (): The method provided directly by the string object is mainly fromCharCode (). The method generates a string based on the Unicode encoding.
Length Property: This property returns the lengths of the strings
charAt () Method: The method returns a character at the specified position (starting at 0), but this method can be substituted with an array subscript
charCodeAt () Method: The method returns the Unicode encoding (decimal representation) of a given position character
concat () method: used to connect two strings. When the method is used, the original string is not affected, and a new string is returned. Multiple strings can be accepted However, in general, string join operations should use the plus (+) operator better.
substring () method, slice method, substr method: These three methods are used to return a substring of a string without altering the original string. They can all accept one or two parameters, and the difference is only the difference in the meaning of the parameter.
SUBSTRING (starting position (with character), terminating position (without characters)): If the first argument is greater than the second one, the substring method automatically replaces the position of the two parameter .
Slice (starting position (with character), terminating position (without characters)): If the first argument is greater than the second argument, the slice method does not automatically swap the parameter position, but returns an empty string .
SUBSTR (The starting position (with characters), how many characters to take): The first argument is the beginning of the substring, and the second argument is the length of the substring.
Negative:
For the substring method, negative numbers are automatically converted to 0.
For the Substr method, a negative number appears in the first argument, indicating the character position (length+ negative value) that is calculated from the tail, and the negative number appears in the second argument, which is converted to 0.
For the slice method, indicates that the character position is calculated from the tail. (length+ negative)
indexOf () method: used to determine the position of a character or characters in a string, starting from the head to match , if there is a second parameter indicating where to start looking (starting from 0), the mismatch returns 1
lastIndexOf () method: used to determine the position of a character or characters in a string, starting at the end of the match , if there is a second parameter indicating where to start looking (starting from 0), the mismatch returns-1
Trim () method: This method is used to remove spaces at both ends of a string
toLowerCase () and toUpperCase () methods: convert strings to lowercase letters and capitals, respectively
Localecompare () Method: Compare String: String1=string2, return 0. String1>string2, returns 1. String1<string2, returns-1. This comparison differs from our usual number size comparisons, nor is it a comparison of strings who are long. But a comparison, if the first is equal, then the second position, as long as the second bit compared to the size without continuing to compare, directly to the size, so the comparison of JS size is a comparison of one:
Replace and search (important, common):
Match () method: search string for matching string
The match method returns an array whose members are the first string to match. If no match is found, NULL is returned.
The return array also has the Index property and the input property, indicating where the matching string starts (starting at 0) and the original string , respectively.
Search () method: returns index directly, starting at the first position, or 1 if no match is found. Non-traversal
Replace () method: Replaces characters, replace (old, new), and only changes the first retrieved character by default.
Use regular expressions to make all the characters change:
Split () method: splits the string split ("by what conditions to cut"), saves the character in the array, and returns a new array. You can accept the second parameter to limit the maximum number of members that are returned to the array ( This method can be used to convert the string to arrays)
If the part that satisfies the split rule is at the beginning or end of the string (that is, there is no other character before or after it), the first or last member of the returned array is an empty string.
Conversion between strings and values:
parseint (String): string converted to int type;
parsefloat (String): string converted to float type;
toString (number): the value is converted to a string;
Reference Link: http://javascript.ruanyifeng.com/stdlib/string.html
A string of JavaScript learning notes