当前位置:网站首页>JS string method summary
JS string method summary
2022-07-27 11:51:00 【Big carp Yu】
I have summarized that it can be used as a query manual
The following two strings are explored , All string methods do not change the original string
let str = '3311 Method collation of strings 1122'
let str1 = ' character string 1'
indexOf(str,startIndex)
Returns the subscript of the position where the formal parameter appears in the caller , Returns if it does not exist -1, The second parameter is to search from the taxi index
// ①indexOf(str,startIndex)
//result==>12 '3311 Method collation of strings 1122'
console.log(str.indexOf(1,5)+'\t'+str)
charAt(index)
Returns the character at the specified index position
//result==> word 3311 Method collation of strings 1122
console.log(str.charAt(4)+'\t'+str)
concat(…args)
Combine multiple characters with the caller to form a new string .!! This method does not change the original string !!
//result==>3311 Method collation of strings 1122 character string 1 3311 Method collation of strings 1122
console.log(str.concat(str1)+'\t'+str)
substring(startIndex,endIndex?)
This method intercepts the string from the specified start index position to the specified end index position , Pay attention to closing on the left and opening on the right , If you do not pass the second parameter, you will intercept from the specified index to the end of the string ( The starting index position can be negative , A negative number represents the reverse from the back to the front startIndex individual ) !! This method does not change the original string !!
//result==> Method collation of strings 1122 3311 Method collation of strings 1122
console.log(str.substring(4)+'\t'+str)
//result==> Method collation of strings
console.log(str.substring(4,12))
substr(startIndex,length)
This method intercepts the number of strings starting from the specified index position as the second parameter ( The starting index position can be negative , A negative number represents the reverse from the back to the front startIndex individual )!! This method does not change the original string !!
//result==> A string of 3311 Method collation of strings 1122
console.log(str.substr(4,4)+'\t'+str)
slice(startIndex,endIndex?)
This method intercepts part of the string from startIndex-endIndex Left closed right away If the second parameter is negative , The character from the specified position to the end of the number from the back to the front will be extracted . If the first argument is negative , There is no output , No mistake. . !! This method does not change the original string !!
//result==> Method collation of strings
console.log(str.slice(4,12)+'\t'+str)
replace( Parameters 1: Regular / character string , Parameters 2: New string / function )
//result==>3391 Method collation of strings 1122
console.log(str.replace('1','9'))
//result==>aaaa Method collation of strings aaaa You can use g Realization replaceAll The effect of
console.log(str.replace(/\d/g,'a'),str)
//result==>1111 Method collation of strings 1111 If the second argument is a function , Will replace the parameter with the return value of the function 1 This function takes four arguments ( Parameters a: Represents the matching parameter 1 Which ones are here a:3,3,1,1,1,1,2,2 Parameters b: On behalf of the parameter a Those subscripts of Parameters c: On behalf of the caller itself, here is str)
console.log(str.replace(/\d/g,(a,b,c)=>1))
replaceAll()
Don't introduce too much replace, The only difference is ,replaceAll All matches will be replaced directly !! This method does not change the original string !!
split(str,num?)
Use the specified separator string to place a Str Object is divided into substring arrays , The second parameter determines how many !! This method does not change the original string !!
//result==> ['3', '3', '1', '1', ' word ', ' operator ', ' strand ', ' Of ', ' Fang ', ' Law ', ' whole ', ' The reason is ', '1', '1', '2', '2']
console.log(str.split(''))
//result==>['3', '3']
console.log(str.split('',2),str)
trim()
Remove white characters at both ends of empty string , The middle space cannot be removed , You can remove all spaces through the above replace Method ,\s Regular table blank characters
//result==>a a a
let trimStr = ' a a a '
console.log(trimStr.trim())
//result==>aaa
console.log(trimStr.replace(/\s/g,''))
match(str)
Return results 1 It returns an array , The array contains the result information of string matching regular expression . Return results 2 If the global match , The matching result will be returned as an array
//result==>[' character string ', index: 4, input: '3311 Method collation of strings 1122', groups: undefined] index It refers to the index that matches the successful start position , Input Property contains the entire string being searched groups: An array of capture groups or undefined( If no named capture group is defined )
console.log(str.match(' character string '))
//result==> ['3', '3', '1', '1', '1', '1', '2', '2']
console.log(str.match(/\d/g))```
边栏推荐
- 局域网SDN技术硬核内幕 12 云网CP的日常恩爱——硬件VXLAN转发平面
- Japan Fukushima waste dump safety monitoring agreement will recognize the "safety" of the sea discharge plan
- ZABBIX custom monitoring items
- Shell脚本文本三剑客之sed
- SQL statement learning and the use of pymysql
- Temporary use of solo, difficult choice of Blog
- compute_class_weight() takes 1 positional argument but 3 were given
- go入门篇 (2)
- 检定和校准的区别
- compute_ class_ weight() takes 1 positional argument but 3 were given
猜你喜欢

Principle of PWM and generation of PWM wave

Weibo comment crawler + visualization

TapNet: Multivariate Time Series Classification with Attentional Prototypical Network

Japan Fukushima waste dump safety monitoring agreement will recognize the "safety" of the sea discharge plan

EfficientNet

Idea: can't use subversion command line client: SVN solution

The first case of monkeypox in pregnant women in the United States: the newborn was injected with immunoglobulin and was safely born

Shell script text three swordsmen sed

【机器学习-白板推导系列】学习笔记---支持向量机和主成分分析法

Temporary use of solo, difficult choice of Blog
随机推荐
意外收获史诗级分布式资源,从基础到进阶都干货满满,大佬就是强!
解决方案:idea project没有显示树状图
Leetcode 03: t58. Length of the last word (simple); Sword finger offer 05. replace spaces (simple); Sword finger offer 58 - ii Rotate string left (simple)
JS-寄生组合式继承
[machine learning whiteboard derivation series] learning notes --- conditional random fields
哈希表 详细讲解
Difference quotient approximation of wechat quotient
w.r.t. ; i.e.; etc.; e. G. what does it mean
go入门篇 (4)
微商的差商近似
Sword finger offer notes: t57 - I. and two numbers of S
Matlab S-function详解
compute_class_weight() takes 1 positional argument but 3 were given
Shell编程之正则表达式(Shell脚本文本三剑客之grep)
SQL statement learning and the use of pymysql
源码编译安装LAMP
Detailed explanation of hash table
Adobe Audition提示 音频输入的采样率与输出设备不匹配——问题解决
Common power supply problems and solutions of Arduino
TapNet: Multivariate Time Series Classification with Attentional Prototypical Network