Methods of type javascript String

1. Basic string inheritance methods valueOf(), toLocaString(), toString(), returns the value of the base string represe...

1. Basic string inheritance methods
valueOf(), toLocaString(), toString(), returns the value of the base string represented by the object

2. Character Method
1.charAt() returns the character that returns the given position as a single string

var str = new String("tengtang") console.log(str.charAt(1)) //"e"

Square bracket notation is also defined to access specific characters in the string

var str = "tengtang"; console.log(str[1]) //"e"

2.charCodeAt() returns the character that returns a given location in a single-character encoding

var str = new String("tengtang") console.log(str.charCodeAt(1)) //"101"

3. String Operation Methods
1.concat(), used to stitch one or more strings together to return the stitched new string.

var str = 'tangtang'; var str_con = str.concat('aaaa'); console.log(str); //'tangtang' console.log(str_con); //'tangtangaaaa';

You can accept any number of parameters, that is, the concat() method can stitch any number of strings

var str = 'tangtang'; var str_con = str.concat('aaaa','hello); console.log(str); //'tangtang' console.log(str_con); //'tangtangaaaahello';

The'+'operator is a more convenient way to stitch strings

2.substr(arg1,arg2), creates a new string based on a substring. The first parameter specifies the starting position, the second parameter is the length of the returned character, and the second parameter is a negative number. The first parameter is added to the length of the current string, and the second parameter is converted to zero.

3.substring(arg1,arg2), the first parameter specifies the starting position, the second parameter indicates where the substring ends, and if the parameter is negative, all negative parameters are converted to zero.Special point: A smaller number will be used as the starting position and a larger number as the ending position.

4.slice(arg1,arg2), the first parameter specifies the start position, the second parameter indicates where the substring ends, and the parameter is negative, adding the negative number to the current string length.

var str = "hello world" var str_length = str.length; //11, string length //When there is only one parameter, the results are consistent console.log(str.substr(3)); //"lo world" console.log(str.substring(3)); //"lo world" console.log(str.slice(3)); //"lo world" //When there are two parameters, the second parameter of substr() has a different meaning, representing the length of the substring console.log(str.substr(3,5)); //"lo wor" console.log(str.substring(3,5)); //"lo" console.log(str.slice(3,5)); //"lo //When the first parameter is negative console.log(str.substr(-3)); //"rld" is equivalent to substr(8) console.log(str.substring(-3)); //"hello world" is equivalent to substring(0) console.log(str.slice(-3)); //"hello world" is equivalent to substring(0) //When the second parameter is negative console.log(str.substr(3,-5)); //"" corresponds to substr(3,0) console.log(str.substring(3,-5)); //"hel" is equivalent to substring(3,0) console.log(str.slice(3,-5)); //"lo" is equivalent to substring(3,6) //When there are two negative parameters console.log(str.substr(-3,-5)); //"" corresponds to substr(8,0) console.log(str.substring(-3,-5)); //"" corresponds to substring(0,0) console.log(str.slice(-3,-5)); //"" is equivalent to slice(8,6)

5 July 2020, 11:15 | Views: 8548

Add new comment

For adding a comment, please log in
or create account

0 comments