String object in JavaScript methods and properties summary
String object mainly for handling text strings.
This article describes a total of 26 ways to follow-up, if any supplement will be updated at any time
Attributes
constructor
Constructor String object, the object is to create a function reference.
length
String length
prototype
String prototype object, can add properties and methods above
var a = 'abc'
a.constructor.prototype.abc = 'aaa'
a.constructor.prototype.sat = ()=> 'hello'
log(a.abc) // aaa
log(a.say()) // hello
method
charAt()
Description: Returns the character at the specified location.
Syntax: str.charAt (Location)
Code:
var str = 'hello word'
log(str.charAt(1))
// 打印 e
charCodeAt()
Description: Returns the Unicode character encoding in the location specified in.
Syntax: str.charCodeAt (Location)
代码:
var str = 'hello word'
log(str.charCodeAt(1))
// 打印 101
concat()
Description: connecting two or more strings and returns the new string.
Syntax: str.concat (str1, str2)
代码:
var str = 'aaa'
var str1 = 'bbb'
var str2 = 'ccc'
log(str.concat(str1, str2))
// 打印 aaabbbccc
fromCharCode()
Description: The Unicode character encoding into
Syntax: String.fromCharCode (65)
代码:
log(String.fromCharCode(65))
// 打印 A
indexOf()
Description: Returns a string value specifies the location of the first occurrence of the string.
Syntax: str.indexOf (string)
代码:
var str = 'hello'
log(str.indexOf('e'))
// 打印 1
includes()
Description: Find a string contains a specified substring. Return true or false
Syntax: str.includes (string)
代码:
var str = 'hello word'
log(str.includes('word'))
// 打印 true
match()
Description: Find find one or more regular expression matching.
Syntax: str.match (regular)
代码:
var str = 'aabbccdd'
log(str.match(/a/g))
// 打印 ["a", "a"]
log(str.match(/a/))
// 打印 ["a", index: 0, input: "aabbccdd"]
repeat()
Description: Specifies the number of times copy the string itself, and return to connect them together.
Syntax: str.repeat (copy number);
代码:
var str = 'abc'
log(str.repeat(3))
// 打印 abcabcabc
replace()
Description: Find matching substring in the string, the string and replacing the regular expression matching.
语法:str.replace(searchValue,newValue)
代码:
var str = 'aabbcc'
log(str.replace('b', 't'))
// 打印 aatbcc
log(str.replace(/b/g, 't'))
// 打印 aattcc
search()
Description: Find the value of regular expression matching the position.
Syntax: str.search (string)
代码:
var str = 'aabbcc'
log(str.search(/c/))
// 打印 4
slice()
Description: extracting fragments of a string, and returns the extracted part in a new string.
Syntax: slice (start, end)
代码:
var str="Hello world!"
log(str.slice(2,9))
// 打印 llo wro
split()
Description: dividing an array of strings is a string.
Syntax: str.split (string)
代码:
var str = 'hello wrod'
log(str.split(''))
// 打印 ["h", "e", "l", "l", "o", " ", "w", "r", "o", "d"]
log(str.split('lo'))
// 打印 ["hel", " wrod"]
startsWith()
Note: Check whether the string starts with substring specified. Ture or false return
Syntax: str.startsWith (string)
Code:
var str = 'hello wrod'
log(str.startsWith('hello'))
// 打印 true
log(str.startsWith('llo'))
// 打印 false
endsWith()
Note: Check whether the string ends with the substring specified. Ture or false return
Syntax: str.endsWith (string)
Code:
var str = 'hello wrod'
log(str.startsWith('hello'))
// 打印 true
log(str.startsWith('llo'))
// 打印 false
substr()
Description: Specifies the number of characters from the start index extraction string. The second parameter is the number of characters to be taken, If not a default character taken to the end, does not change the source string.
Syntax: str.substr (start, length)
Code:
var str = 'hello wrod'
log(str.substr(1, 3))
// 打印 ell
log(str.substr(1))
// 打印 ello wrod
substring()
Description: The character string between two specified index extraction. Between the character segments taken from a location to another. If the second parameter is omitted, then the substring will always return to the end of the string.
语法:string.substring(from, to)
Code:
var str = 'hello wrod'
log(str.substring(1))
// 打印 ello wrod
log(str.substring(1, 3))
// 打印 el
toLowerCase()
Description: to convert the string to lowercase.
Syntax: str.toLowerCase ()
Code:
var str = 'AAAbbbCCCdddEEE'
log(str.toLowerCase())
// 打印 aaabbbcccdddeee
toUpperCase()
Note: to convert a string to uppercase.
Syntax: str.toUpperCase ()
Code:
var str = 'AAAbbbCCCdddEEE'
log(str.toUpperCase())
// 打印 AAABBBCCCDDDEEE
trim()
Description: removing the blank on either side of the string
Syntax: str.trim ()
Code:
var str = ' AAAbbbCCCdddEEE '
log(str.trim())
// 打印 AAAbbbCCCdddEEE
toLocaleLowerCase()
Description: According to the local host locale string converted to lowercase.
Syntax: str.toLocaleLowerCase ()
Code:
var str = 'AAAbbbCCCdddEEE'
log(str.toLocaleLowerCase())
// 打印 aaabbbcccdddeee
toLocaleUpperCase()
Description: According to the local host locale string converted to uppercase.
Syntax: str.toLocaleUpperCase ()
Code:
var str = 'AAAbbbCCCdddEEE'
log(str.toLocaleUpperCase())
// 打印 AAABBBCCCDDDEEE
valueOf()
Description: returns the original value of a string object. valueOf () method is usually performed automatically in the background by the JavaScript call, rather than explicitly in the code.
Syntax: str.valueOf ()
Code:
var str = 'AAAbbbCCCdddEEE'
log(str.valueOf())
// 打印 AAAbbbCCCdddEEE
toString()
Description: Returns a string.
Syntax: str.toString ()
Code:
var str = 'AAAbbbCCCdddEEE'
log(str.toString())
// 打印 AAAbbbCCCdddEEE
tart pads ()
Description: string from the filling head. (ES6)
Syntax: str.padStart (targetLength, string)
Code:
var str = '123'
log(str.padStart(5, '0'))
// 打印 00123
padEnd()
Description: string filled in the tail. (ES6)
Syntax: str.padEnd (targetLength, string)
Code:
var str = '123'
log(str.padEnd(5, '0'))
// 打印 12300
raw()
Description: raw () function is a template tag string, is used to obtain a measure of the original template literal string.
callSite
A template string "call site object"
...substitutions
Any optional parameter indicating the value of the interpolation expression corresponding to an arbitrary number. templateString template string.
Syntax: String.raw (callSite, ... substitutions)
Code:
var str = 'abcd'
log(String.raw`hel${str}lo`)
// 打印 helabcdlo