[JavaScript] A complete collection of common methods of JavaScript String objects, such as search, concat, includes, slice, replace, etc. (including functions, syntax, parameter analysis, and detailed examples)

1. charAt() method

Function: Return the character at the specified position
Syntax:string.charAt(index)

parameter describe
index required. A number representing a position in a string, i.e. the position of a character in the string

example:

let str="hello"
let newStr=str.charAt(1)
console.log(newStr)   //输出结果为e

2. charCodeAt() method

Function: Return the Unicode encoding of the character at the specified position, and the return value is an integer between 0 and 65535
Syntax:string.charCodeAt(index)

parameter describe
index required. A number representing a position in a string, that is, the subscript of a character in the string.

example:

let str="hello"
let code=str.charCodeAt(1)
console.log(code)   //输出结果为101

3. concat () method

Role: used to concatenate two or more strings
Syntax:string.concat(string1, string2, ..., stringX)

parameter describe
string1, string2, …, stringX required. One or more String objects to be concatenated into one String.

example:

let str1="hello"
let str2="world"
let str3="!!!"
let newStr1=str1.concat(str2)
console.log(newStr1)//两个字符串相连输出结果:hello world
let newStr2=str1.concat(str2,str3)
console.log(newStr2)//三个字符串相连输出结果:hello world!!!

4. endsWith() method

Function: used to judge whether the current string ends with the specified substring ( 区分大小写)
syntax:string.endsWith(searchvalue, length)

parameter describe
searchvalue Required, the substring to search for.
length optional. Set the length of the string. The default is the original string length string.length.

example:

let str="hello world";
let returnValueA=str.endsWith("world") 
console.log(returnValueA)   //返回值true
let returnValueB=str.endsWith("world",5) 
console.log(returnValueB)   //返回值false
let returnValueC=str.endsWith("World")
console.log(returnValueC)   //返回值false

5. fromCharCode() method

Function: Accept a specified Unicode value, and then return a string
Syntax:String.fromCharCode(n1, n2, ..., nX)

parameter describe
n1, n2, …, nX required. One or more Unicode values, which are the Unicode encodings of the characters in the string to be created.

example:

let n = String.fromCharCode(72,69,76,76,79);
console.log(n) //输出结果为:HELLO

6. indexOf() method

Function: Returns the position where a specified string value first appears in the string, or -1 if no matching string is found.
grammar:string.indexOf(searchvalue,start)

parameter describe
searchvalue required. Specifies the string value to be retrieved.
start Optional integer parameter. Specifies the position in the string to start searching. Its valid values ​​are 0 to string Object.length - 1. If this parameter is omitted, the search will start from the first character of the string.

example:

let str="hello world";
let returnValueA=str.indexOf("world") 
console.log(returnValueA)   //输出结果为:6
let returnValueB=str.indexOf("world",7) 
console.log(returnValueB)   //输出结果为:-1
let returnValueC=str.indexOf("World") 
console.log(returnValueC)   //输出结果为:-1

7. includes() method

Function: Determine whether the string contains the specified substring. Returns true if a matching string is found, otherwise returns false
Syntax:string.includes(searchvalue, start)

parameter describe
searchvalue required, the string to look for
start Optional, set the position to start searching from, the default is 0.

example:

let str="hello world";
let returnValueA=str.includes("world") 
console.log(returnValueA)   //输出结果为:true
let returnValueB=str.includes("World") 
console.log(returnValueB)   //输出结果为:false

8. lastIndexOf() method

Function: It can return the last occurrence position of a specified string value. If the second parameter start is specified, the specified position in a string will be searched from back to front.
Note:
A. This method will retrieve the string from the back to the front, but the return is the last occurrence of the substring calculated from the starting position (0). to see if it contains a string. The search starts at the start of the string or the end of the string (when start is not specified). Returns -1 if no matching string is found.
B.lastIndexOf() method is case sensitive!
grammar:string.lastIndexOf(searchvalue,start)

parameter describe
searchvalue required. Specifies the string value to be retrieved.
start Optional integer parameter. Specifies the position in the string to start searching. Its valid values ​​are 0 to stringObject.length - 1. If this parameter is omitted, the search will start from the last character of the string.

example:

let str="hello world"
let x=str.lastIndexOf("l")
console.log(x);//输出结果为:9
let y=str.lastIndexOf("l",1)
console.log(y);//输出结果为:-1

9. match() method

Function: The specified value can be retrieved within the string, or a match of one or more regular expressions can be found.
Syntax: string.match(regexp)
Example:

parameter describe
regexp required. A RegExp object specifying the pattern to match. If the parameter is not a RegExp object, you need to first pass it to the RegExp constructor to convert it to a RegExp object.

example:

 let str="HELLO world"
let x=str.match(/L/gi)
console.log(x);//输出结果:['L', 'L', 'l']

10.repeat() method

Function: Copy the specified number of times of string
Syntax:string.repeat(count)

parameter describe
count Returns a string copied the specified number of times and concatenated.

example:

 let str="hello world "
 let x=str.repeat(2)
 console.log(x);//输出结果为:hello world hello world 

11. replace() method

Function: Used to replace some characters with other characters in a string, or replace a substring that matches a regular expression.
grammar:string.replace(searchvalue,newvalue)

parameter describe
searchvalue must. A RegExp object specifying a substring or pattern to replace. Note that if the value is a string, it is used as the literal text pattern to be retrieved, rather than being converted to a RegExp object first.
newvalue required. a string value. Specifies replacement text or a function that generates replacement text.

example:

let str="hello world"
let x=str.replace(/H/i,"TEST")
console.log(x);//使用正则表达式输出结果:TESTello world
let y=str.replace("H","TEST")
console.log(y);//输出结果为:hello world
let z=str.replace("h","TEST")
console.log(z);//输出结果为:TESTello world

12. replaceAll() method

Function: Used to replace some characters with other characters in a string, or replace a substring that matches a regular expression, this function will replace all matched substrings.
grammar:const newStr = str.replaceAll(regexp|substr, newSubstr|function)

parameter describe
regexp must. A RegExp object specifying a substring or pattern to replace. Note that if the value is a string, it is used as the literal text pattern to be retrieved, rather than being converted to a RegExp object first. When using a regex, you must set the global ("g") flag, otherwise, it will raise a TypeError: "replaceAll must be called with a global RegExp".
newSubstr function

example:

let str="hello world!HELLO WORLD"
let x=str.replace(/hello/ig,"TEST")
console.log(x);//输出结果为:TEST world!TEST WORLD
let y=str.replace("HELLO","TEST")
console.log(y);//输出结果为:hello world!TEST WORLD
let z=str.replace("hello","TEST")
console.log(z);//输出结果为:TEST world!HELLO WORLD

13. search() method

Function: Used to retrieve the specified substring in the string, or retrieve the substring that matches the regular expression. Returns -1 if no matching substring is found.
grammar:string.search(searchvalue)

parameter describe
searchvalue must. The string or regular expression to look for.

example:

let str="hello world"
let x=str.search("hello")
console.log(x);//输出结果:0
let y=str.search(/W/i)
console.log(y);//输出结果:6

14. slice() method

Function: A certain part of the string can be extracted, and the extracted part can be returned as a new string. Use the start (inclusive) and end (exclusive) parameters to specify the part of the string to extract. The position of the first character in the start parameter string is 0, the position of the second character is 1, and so on. If it is a negative number, it means how many strings are intercepted from the end, and slice(-2) means to extract the penultimate character in the original array. The second element to the last element (inclusive). If the end parameter is negative, -1 refers to the position of the last character of the string, -2 refers to the penultimate character, and so on.
grammar:string.slice(start,end)

parameter describe
start must. The starting subscript of the segment to be extracted, the first character position is 0. If negative, intercept from the end.
end optional. Subscript immediately following the end of the segment to be truncated. If this parameter is not specified, the substring to be extracted includes the string from start to the end of the original string. If this parameter is negative, it specifies the position from the end of the string. slice(-2) means to extract the penultimate element to the last element (including the last element) in the original array.

example:

let str="hello world"
let x=str.slice(2)
console.log(x);//输出结果:llo world
let y=str.slice(2,3)
console.log(y);//输出结果:l
let z=str.slice(-1)
console.log(z);//输出结果:d

15. split() method

Function: used to split a string into a string array.
grammar:string.split(separator,limit)

parameter describe
separator optional. A string or regular expression to split the string Object from where specified by this parameter.
limit optional. This parameter specifies the maximum length of the returned array. If this parameter is set, no more substrings will be returned than the array specified by this parameter. If this parameter is not set, the entire string will be split regardless of its length.

example:

let str="hello world"
let x=str.split()
console.log(x);//输出结果:['hello world']
let y=str.split("")
console.log(y);//输出结果:['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
let z=str.split("e")
console.log(z);//输出结果:['h', 'llo world']
let t=str.split("",3)
console.log(t);//输出三个数组的结果为:['h', 'e', 'l']

16. startsWith() method

Function: used to detect whether the string starts with the specified substring. Returns true if it starts with the specified substring, otherwise false.
grammar:string.startsWith(searchvalue, start)

parameter describe
searchvalue Required, the string to look for.
start 可选,查找的开始位置,默认为 0。

例子:

let str="hello world"
let x=str.startsWith("llo")
console.log(x);//输出结果:false
let y=str.startsWith("llo",2)
console.log(y);//输出结果:true

17.substr() 方法

作用:可提取字符串的某个部分,并以新的字符串返回被提取的部分。
语法:string.substr(start,length)

参数 描述
start 必需。要抽取的子串的起始下标。必须是数值。如果是负数,那么该参数声明从字符串的尾部开始算起的位置。也就是说,-1 指字符串中最后一个字符,-2 指倒数第二个字符,以此类推。
length 可选。子串中的字符数。必须是数值。如果省略了该参数,那么返回从 stringObject 的开始位置到结尾的字串。

例子:

let str="hello world"
let x=str.substr(3)
console.log(x);//输出结果:lo world
let y=str.substr(3,5)
console.log(y);//输出结果:lo wo

18.substring() 方法

作用:用于提取字符串中介于两个指定下标之间的字符。返回的子串包括开始处的字符,但不包括结束处的字符。
语法:string.substring(from, to)

参数 描述
from 必需。一个非负的整数,规定要提取的子串的第一个字符在 string Object 中的位置。
to 可选。一个非负的整数,比要提取的子串的最后一个字符在 string Object 中的位置多 1。如果省略该参数,那么返回的子串会一直到字符串的结尾。

例子:

let str="hello world"
let x=str.substring(3)
console.log(x);//输出结果为:lo world
let y=str.substring(3,7)
console.log(y);//输出结果为:lo w

19.toLowerCase() 方法

作用:用于把字符串转换为小写。
语法:string.toLowerCase()
例子:

let str="HELLO world"
let x=str.toLowerCase()
console.log(x);//输出结果:hello world

20.toUpperCase() 方法

作用:用于把字符串转换为大写。
语法:string.toUpperCase()
例子:

let str="HELLO world"
let x=str.toUpperCase()
console.log(x);//输出结果:HELLO WORLD

21.trim() 方法

作用:去除字符串的头尾空格
语法:string.trim()
例子:

let str=" HELLO world "
let x=str.trim()
console.log(x);//输出结果:HELLO world

22.valueOf() 方法

作用:可返回 String 对象的原始值。valueOf() 方法通常由 JavaScript 在后台自动进行调用,而不是显式地处于代码中。
语法:string.valueOf()
例子:

let str="hello world"
let x=str.valueOf()
console.log(x);//输出结果:hello world

23.toString() 方法

作用:返回一个表示 String 对象的值。
语法:string.toString()
例子:

let str="hello world"
let x=str.toString()
console.log(x);//输出结果:hello world

24.toLocaleLowerCase() 方法

作用:根据本地主机的语言环境把字符串转换为小写。
语法:string.toLocaleLowerCase()
例子:

let str="HELLO world"
let x=str.toLocaleLowerCase()
console.log(x);//输出结果:hello world

25.toLocaleUpperCase() 方法

作用:根据本地主机的语言环境把字符串转换为大写。
语法:string.toLocaleUpperCase()
例子:

let str="HELLO world"
let x=str.toLocaleUpperCase()
console.log(x);//输出结果:HELLO WORLD

结论

  这篇文章主要总结了JavaScript String对象常用的一些方法,如果哪里有错误或者不完善的地方,欢迎各位指教,谢谢!

Guess you like

Origin blog.csdn.net/m0_46533551/article/details/129174843