js string basic data type

String is a basic data type and also a basic packaging type. Generally speaking, basic types should not have methods. In order to allow us to achieve more intuitive operations, when calling methods of basic packaging types, the background will automatically complete a series of processing:

(1) Create an instance of String type (2) Call the specified method on the instance (3) Destroy the instance

var s1 = "abc"
s2 = s1.substring(2)

相当于执行了以下代码:

var s1 = new String('abc')
var s2 = s1.substring(2)
s1 = null

The main difference between the reference type and the basic packaging type
is the lifetime of the object. The instance of the reference type created with the new operator is kept in memory until the execution flow leaves the current scope. The automatically created objects of the basic packaging type only exist at the moment of execution of a line of code, and then are destroyed immediately, so we cannot add properties and methods to the basic types while the code is running

var str1 = '123';
console.log(typeof str1) //string
 
var str2 = new String('aaa')
console.log(typeof str2) //object
 
var str3 = new Object('111')
console.log(typeof str3) //object
console.log(str3 instanceof String) //true
console.log(str3 instanceof Object) //true

String common methods

var str="hello world";
 
str.charAt(1) //e 返回给定位置的字符
str.charcodeAt(1) //101 返回给定位置字符的字符编码
str[1] //e ie8+

cancat() //Can accept any number of parameters spliced ​​into a new string, but will not change the original string

slice() //Intercept a string, accept one or two parameters (start position and end position), and add the negative value to the length of the string when accepting a negative value
substring() //Intercept a string, accept one or two Two parameters (start position and end position, the smaller parameter will be used as the starting position), when accepting a negative value, the negative parameter will be converted to zero
substr() //Intercept the string, accept one or two parameters (start Position and the number of characters intercepted), when accepting a negative value, the first negative parameter will be added to the string length, and the second negative parameter will be converted to 0
indexOf() // Accept two parameters, to find The substring and the search starting point (optional), the return position is found, and the return -1 is not found.
lastIndexOf() //Search from the end of the array
trim() //Remove the spaces in the prefix and suffix and return a string A copy of the original string unchanged

toLowerCase() //turn to lowercase
toUpperCase() //turn to uppercase
toLocaleLowerCase() //turn to lowercase, method for region
toLocaleUpperCase() //turn to uppercase, method for region

match() //Receive a parameter, a regular expression or a RegExp object
search() //Accept a regular, return the index of the first match in the string, without returning -1
replace() //Replace the string. Accepts two parameters, the first is a string or RegExp object,

//the second parameter is a string or function. If the first parameter is a string,
// then only the first substring will be replaced. The only way to replace all is to provide a
//regular expression that specifies the global g flag

//replace() method The second parameter can also be a function
function(match,...,pos,originalText){
match //The matching item of the pattern
... //When the regular expression defines multiple capture groups, it is the second, third...match Item
pos //The position in the string of the matching item of the pattern
originalText //Original string
}
split() //Split the string and return an array. The first parameter takes one separator (RegExp object or can be a string) ,
// optional second parameter specifies the size of the array returned
localeCompare () / / compare two strings, if the letter string The table should be sorted before the string parameter and return a negative number. Equal returns 0, then returns a positive number

String.fromCharcode() // The static method of the constructor itself, which receives one or more character codes and converts them into a string, which is the opposite of charCodeAt **

Guess you like

Origin blog.csdn.net/weixin_48895522/article/details/109479743