Method of String 1. String: The contents of the enclosed or double quotation marks in JS are strings;
var t = "true"; Console.log (typeof T); // "string"Console.log (typeoftrue); // "Boolean" var str = "Yyy./niha"; var s = ' www '; var str = "HelloWorld";
2. Index: In the string, each character has a corresponding index, the index is a number, starting from 0;
Console.log (Str[3]); // "F";
3.length: String has a length property, and the property value represents the number of characters of the current string;
Console.log (str.length); // gets the last character of a string;Console.log (str[str.length-1]);
4. String operation; +-*/-*/: The string is converted to a number before the calculation
Console.log ("6"-2); // 4Console.log ("5"/"4")//1.25Console.log ("5px"-"4")// NaNconsole.log ( True* "4")// 4
- Any number and Nan calculation, the result is Nan;
- Any number and undefined operation, get is also Nan;
+: string concatenation;
Console.log ("6px" +undefined); Console.log (NaN+ "undefined"); []: An empty array is called by default to convert to an empty string when stitching, and then stitching; vartypeoftruenull + undefined+{}; // "numbertruenullundefined"Console.log (a);
5. String method
- Index
- Length
- String operations
1. toUpperCase: Turn lowercase letters into uppercase
str.touppercase () var str1 = "HELLO"
2.toLowerCase turn uppercase to lowercase
Console.log (Str1.tolowercase ());
3.charAt: Get characters by index
Console.log (Str.charat (4));
4.CHARCODEAT: Obtain Unicode encoding of corresponding characters by index;
A
-Z: 97–122 0-9:48-57 A-Z: 65-90console.log (str.charcodeat (0));
5.SUBSTR: Intercept substr (m,n) starts from index m and intercepts n characters;
substr (m): Intercept from index m to end Console.log (STR.SUBSTR (2)); Console.log (Str.substr (2,5));
6.substring:substring (M,n): Starting from index m, intercept to index n, not containing n;
when n is negative, M is truncated to the beginning; negative numbers are not supported; Console.log (Str.substring (2, 5)); Console.log (Str.substring (5,-1));
7.slice (m,n): substring; Start at index m, intercept to index n, not contain n
support for interception of negative numbers, Console.log (Str.slice (3, 7)), Console.log (Str.slice (3,-1)), Console.log (Str.slice (3, 0)
);
8.INDEXOF: detects the index position of the first occurrence of a character in a string;
returns the index if the string does not exist, returns -1;console.log (Str.indexof ("E")),//4console.log (Str.indexof ("w"));//-1
9.LASTINDEXOF: detects the index position of the last occurrence of a character in a string;
returns the index, if the string does not exist, returns -1;console.log (Str.lastindexof ("n")), Console.log (Str.lastindexof ("K
"));
10.split: Separates each item in the array by a specific character;
Console.log (Str.split ("")); var str = "Zhufengpeixun";
11.replace: Replace the original string, replace the old character with the new character
Console.log (Str.replace ("U", "M"). Replace ("U", "M"); string. replace (OLDSTR,NEWSTR); Console.log (str); var str1 = " Hellohello "; Console.log (Str1.replace (" Hello "," HelloWorld "). Replace (" Hello "," HelloWorld ");//" Helloworldworldhello "
12.concat: Stitching
var str = "AER"; Console.log (Str.concat ("123"));//"aer123"
13.trim: Go to Space: Remove the left and right spaces in the string;
Trimleft: Go to the left of the string trimright: Go to the space to the right of the string; var str = "66yy yww"; Str.trim ()
JavaScript Basics (method of String)