The underlying string is stored in the form of an array of characters
[“H” ”e” ”l” ”l” ”o”]
length attribute
- You can get the length of the string
//Create a string
var str = "Hello World"
console.log(str.length);
console.log(str[5]);
charAt( )
- Can return the character at the specified position in the string
- Get the specified character according to the index
//charAt() can return the character at the specified position in the string
var result = str.charAt(1);
console.log(result); Get the character with index 1
charCodeAt( )
- Can return the unicode encoding of the character at the specified position in the string
//charCodeAt() returns the unicode encoding of the character at the specified position in the string
var result2 = str.charCodeAt(0);
console.log(result2); returns the Unicode encoding of the character at index 0
String.formCharCode( )
- Characters can be obtained according to character encoding
// String.fromCharCode() can get the character according to the character code
var result3 = String.fromCharCode(72);
console.log(result3); returns the character with Unicode code 72
Concat()
- You can concatenate two or more strings
- Same as +
- Will not affect the original string
//concat() can concatenate two or more strings
var str = "Hello World"
var result4 = str.concat("Hello, goodbye");
console.log(result4); concatenate two strings
indexOf( )
- This method can retrieve whether a string contains specified content
- If the string contains the content, the index of its first occurrence will be returned
- If the specified content is not found, it will return -1
- You can specify a second parameter to specify the location to start searching
//indexOf() can retrieve whether a string contains the specified content (from front to back)
var str = "Hello World"
var result5 = str.indexOf("e",1);
console.log(result5); from Find the character with e at the index of 1
lastIndexOf( )
- The usage of this method is the same as indexOf()
- The difference is that indexOf() is to find from front to back
lastIndexOf() is to find from back to front - You can also specify where to start the search
//lastIndexOf() can retrieve whether a string contains the specified content (search from back to front )
var str = "Hello World"
var result6 = str.lastIndexOf("l",5);
console.log(result6) from Start at index 5 to find the character that is l
Slice( )
- Can intercept the specified content from the string
- Does not affect the original string, but returns the intercepted content
- parameter
- Index of the first start position (including start position)
- The index of the second end position (not including the end position)
- If the second parameter is omitted, all the following will be intercepted
- You can also pass a negative number as a parameter, and the negative number will be calculated from the back
//slice() can intercept the specified content from the string
var str = "abcdefg";
var result = str.slice(0,3);
console.log(result); intercept the characters whose index is from 0 to 3
subString( )
It can also be used to intercept a string, similar to slice()
- parameter
- Index of the first start position (including start position)
- The index of the second end position (not including the end position)
- The difference is that this method does not accept negative value as a parameter
, if a negative value is passed, the default 0 - And it also automatically adjusts the position of the parameter, if the second parameter is less than the first parameter, it will automatically exchange
subStr( )
- Used to intercept the string
- parameter
- Index of the first start position (including start position)
- The second intercepted quantity
//substr() intercepts the string, the second parameter indicates the number of interceptions
var str = "abcdefg";
var result = str.substr(3,2);
console.log(result); starting from the character whose index is 3 Intercept 3 characters
split( )
- You can split a string into an array
- parameter:
- A string is required as a parameter, and the array will be split according to the string
//split() can split a string into an array
var str = "abc,def,ghi,jkl";
result = str.split(",");
console.log(typeof result);
console.log( result);
console.log(result.length);
toUpperCase( )
- Can convert the string to uppercase and return
//toUpperCase() can convert a string to uppercase
var str = "abcdefg";
result = str.toUpperCase();
console.log(result);
toLowerCase( )
- Can convert the string to lowercase and return
var str = “ABCDEFG”;
result = str.toLowerCase();
console.log(result);