当前位置:网站首页>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))```
边栏推荐
- Pytorch shows the summary like tensorflow
- Sword finger offer notes: t58 - I. flip word order
- TLC549Proteus仿真&Sallen-Key滤波器&AD736Vrms到DC转换&Proteus查看51寄存器值
- 解决@OneToMany查询陷入循环引用问题
- Firewall firewall
- Matlab S-function详解
- Synchronous use reference of the new version of data warehouse (for beginners)
- 一些MathType常用快捷键
- Leetcode 02: sword finger offer 58 - I. flip the word order (simple); T123. Verify palindrome string; T9. Palindromes
- Principle of PWM and generation of PWM wave
猜你喜欢

SMA TE: Semi-Supervised Spatio-Temporal RepresentationLearning on Multivariate Time Series

Maker Hongmeng application development training notes 02

Temporary use of solo, difficult choice of Blog

NPM step pit

Wilcoxon rank-sum 和 signed-rank

PWM的原理和PWM波的产生

origin如何作一张图中多张子图是柱状图(或其他图)

Newton-Raphson迭代法

JS-寄生组合式继承

【机器学习-白板推导系列】学习笔记---条件随机场
随机推荐
Solution of digital tube flash back after proteus8 professional version cracking
[untitled] multimodal model clip
局域网SDN技术硬核内幕 12 云网CP的日常恩爱——硬件VXLAN转发平面
w.r.t. ; i.e.;etc.;e.g.是什么意思
Keil MDK编译出现..\USER\stm32f10x.h(428): error: #67: expected a “}“错误的解决办法
pytorch和tensorflow一样展示summary
Sword finger offer notes: T53 - ii Missing numbers from 0 to n-1
npm踩坑
Database cli tool docker image
LNMP架构搭建(部署Discuz论坛)
意外收获史诗级分布式资源,从基础到进阶都干货满满,大佬就是强!
Shell脚本文本三剑客之sed
数据包传输:应用层-内核-硬件
【机器学习-白板推导系列】学习笔记---概率图模型和指数族分布
剑指 Offer 笔记: T45. 把数组排成最小的数
解决方案:idea project没有显示树状图
Idea: can't use subversion command line client: SVN solution
Sword finger offer notes: t57 - ii Continuous positive sequence with sum s
【机器学习-白板推导系列】学习笔记---条件随机场
为什么TCP三次握手的时候ACK=Seq+1