当前位置:网站首页>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转发平面
- LAN SDN hard core technology insider 23 looking forward to the future - RDMA (Part 1)
- 本地虚拟机初始化脚本
- Shell script text three swordsmen sed
- Beyond Compare 3 下一个差异段/向下搜索箭头 找不到了
- Local virtual machine initialization script
- Summary of leetcode SQL exercises (MySQL Implementation)
- A possibility that ch340 module cannot be recognized / burned
- 开源flink有写holo的Datastream connector或者flink sql conn
- compute_ class_ weight() takes 1 positional argument but 3 were given
猜你喜欢

Could not load dynamic library ‘libcudnn.so.8‘;
![[unity entry program] creator kitfps: first person shooting 3D game](/img/2b/78b535973b2898f53752ceeb25ef01.png)
[unity entry program] creator kitfps: first person shooting 3D game

EfficientNet

Shell脚本文本三剑客之awk

暂用 Solo,博客选择困难

微博评论爬虫+可视化

Could not load dynamic library ‘libcudnn.so.8‘;

N ¨UWA: Visual Synthesis Pre-training for Neural visUal World creAtionChenfei

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

Solution of digital tube flash back after proteus8 professional version cracking
随机推荐
Leetcode 04: T26. Delete duplicate items in the sorting array (simple); Sword finger offer 67. convert the string to an integer (medium); Interview question 01.08. zero matrix (simple)
Shell脚本文本三剑客之sed
Leetcode 02: sword finger offer 58 - I. flip the word order (simple); T123. Verify palindrome string; T9. Palindromes
剑指 Offer 笔记: T57 - I. 和为 s 的两个数字
82. (cesium home) cesium points move on 3D models
一些MathType常用快捷键
求不同采样周期下的传递函数有限零点
torch‘ has no attribute ‘inference_mode‘
compute_ class_ weight() takes 1 positional argument but 3 were given
VSCode复制代码时去掉样式/语法高亮/代码高亮/黑色背景
局域网SDN技术硬核内幕 13 从局域网到互联网
本地虚拟机初始化脚本
JS-寄生组合式继承
LNMP架构搭建(部署Discuz论坛)
Synchronous use reference of the new version of data warehouse (for beginners)
TapNet: Multivariate Time Series Classification with Attentional Prototypical Network
Tlc549proteus simulation &sallen key filter &ad736vrms to DC conversion &proteus view 51 register value
局域网SDN硬核技术内幕 23 展望未来——RDMA(上)
为什么TCP三次握手的时候ACK=Seq+1
Beyond Compare 3 下一个差异段/向下搜索箭头 找不到了