Self-learning record (always updated 🌺) string of fourteen methods: charAt(index), charCodeAt(index), concat(STR, STR…) , indexOf(item, index), lastIndexOf(item,index), subString (stratIndex, stopIndex), str.substr(start, Length), split(separator,howmany), toUpperCase(), toLowerCase(), startsWith(), endsWith(), includes(), repeat (), trim(), slice()

1. Str.charat (n) returns characters at the specified position in the string

N If the value is not between 0 and str.length-1, an empty string is returned

var str = 'maomao love you'
str.charAt(3) // m
str.charAt(20) / /"
Copy the code

2. Str.charcodeat (n) returns the character encoding at the specified position in the string

var str = 'maomao love you'
str.charCodeAt(3) / / 109
str.charCodeAt(20) // NaN
Copy the code

The concat() method is used to concatenate two or more strings.

str.concat(stringX,stringX,… , stringX) : required. One or more string objects to be concatenated into a string.

var str = '123'
str.concat('Hold hands'.'456'.'Hold your head up') // "123 hold hands 456 lift head"
Copy the code

4. IndexOf () and lastIndexOf ()

Str.indexof (value,index) returns the position at which value first appears in the string STR, starting at the start position, or -1 if none exists. The value required. Specifies the string value to be retrieved str.lastIndexof (value,fromindex) returns the last occurrence of value in the string STR, looking forward from the end, or -1 if none exists.

5. Str.substring (start, stop) is used to extract characters between two specified subscripts in a string

A new string subcontent is all characters from start to stop-1, with length= stop-start.

6. The str.substr(start,length) method extracts a specified number of characters from the start subscript in the string.

The content of a new string is the length of characters starting at start (including the character referred to by start). If length is not specified, the string returned contains characters from start to the end of stringObject.

7. The split() method splits a string into an array of strings.

str.split(separator,howmany)
Copy the code

8. The toUpperCase() method is used to convert a string toUpperCase and the toLowerCase() method is used to convert a string toLowerCase.

str.toUpperCase()
str.toLowerCase()
Copy the code

9. The startsWith (), endsWith ()

10. includes()

11. Repeat ()

12. Trim () Removes Spaces at both ends of the string

13. Replace () replaces the string of STR

14. slice() str.slice(start[,end])

There are three ways to extract and slice a string, such as:

1. Use the slice () :
var str = 'learning work'
str.slice(1.3) // ea
Copy the code
2. Use the substring () :
var str = 'learning work'
str.substring(1.3) // ea
Copy the code
Use substr(start, length):
var str = 'learning work'
str.substr(1.3) // ear
Copy the code

Unlike the first and second methods, the substr() second argument represents the maximum length of the string to be cut, as shown in the result above

🌺 test drive 🌺

1. Capitalize the first letter of each word in the string separated by a space and write the general method.
var str = 'I believe I am the best'
function upStr (str) {
    // do something
}
upStr(str) // `I Believe I Am The Best`
Copy the code
var str = 'I believe I am the best'
function upStr (str) {
	var _str = str.split(' ').reduce((prev, next) = > {
		prev = prev + next.charAt(0).toUpperCase() + next.substring(1) + ' '
		return prev
	}, ' ')
	return _str
}
upStr(str) // "I Believe I Am The Best "
Copy the code
Wefsdfsfajspangfsetwdsfs = wefsdfsfajspangfsetwdsfs = wefsdfsfajspangfsetwdsfs = wefsdfsfajspangfsetwdsfs