当前位置:网站首页>15 methods in "understand series after reading" teach you to play with strings
15 methods in "understand series after reading" teach you to play with strings
2022-07-05 11:32:00 【InfoQ】
1. Remove leading and trailing spaces from the string —— trim()
string.trim()
let str = " White is not white , Black is not black , I ... I don't like you "
let result = str.trim()
console.log(result); // Console printing :" White is not white , Black is not black , I ... I don't like you "
2. Replace specified character with string ( strand )—— replace()
replace()
string.replace(searchvalue,newvalue)
let str = " You know what? ? There is... In the world 60 Hundreds of millions of people , The universe has 60 Trillion asteroids , You are 10000 times more precious than asteroids "
let result1 = str.replace(" Billion "," individual ")
let result2 = str.replace(/ Billion /g," individual ")
console.log(result1) // Console printing :" You know what? ? There is... In the world 60 personal , The universe has 60 Trillion asteroids , You are 10000 times more precious than asteroids "
console.log(result2) // Console printing :" You know what? ? There is... In the world 60 personal , The universe has 60 Ten thousand asteroids , You are 10000 times more precious than asteroids "
3. String merge —— concat()
string.concat()
let str1 = " I have many sweet words ,"
let str2 = " But I dare not say it when I face you ."
let result = str1.concat(str2)
console.log(result) // Console printing :" I have many sweet words , But I dare not say it when I face you ."
4. String to array —— split()
split
string.split()
string.split(/[*]/)
let str = " Zhang San , Li Si ; Wang Wu "
let result1 = str.split(',')
let result2 = str.split(/[,;]/)
console.log(result1) // Console printing :[" Zhang San ", " Li Si ; Wang Wu "]
console.log(result2) // Console printing :[" Zhang San ", " Li Si ", " Wang Wu "]
5. String to array —— [...string]
split()
...
[...string]
let str = ' This is a string string'
let arr = [...str]
console.log(arr) // Console printing :[" this ", " yes ", " One ", " individual ", " word ", " operator ", " strand ", "s", "t", "r", "i", "n", "g"]
6. String inversion —— [...string].reverse().join("")
[...string].reverse().join("")
let str = " Pole reversal , The tornado destroyed the parking lot !"
let result = [...str].reverse().join("")
console.log(result) // Console printing :! The car stops and destroys the wind and tornado , Reverse pole two
7. Multiple copies of strings —— repeat ()
string.repeat(n)
let str1 = ' Copy '
let result = str1.repeat(2)
consol.log(result) // Console printing : Copy copy
let str2 = '10'
let result = str2.repeat(5)
console.log(result) // Console printing :1010101010
8. Whether the string contains a character ( strand )—— search()
string.search(searchvalue)
let str = " The night is very good today , The moon is also very round , The only regret is that , I don't see the moon from your window ."
let result1 = str.search(" The moon ")
let result2 = str.search(/[,.]/)
console.log(result1) // Console printing :8
console.log(result2) // Console printing :7
9. Whether the string contains a character ( strand )—— includes()
includes()
search()
includes()
search()
includes()
string.includes(searchvalue, start)
let str = " There is no cool wind in summer , There is no warm winter sun , They just happen to appear at the right time "
let result1 = str.includes(" In winter ")
let result2 = str.includes(" In winter ",20)
console.log(result1); // Console printing :true
console.log(result2); // Console printing :false
10. The position of the string value specified in the string at the first or last occurrence —— indexOf() and lastIndexOf()
indexOf()
lastIndexOf()
string.indexOf(searchvalue,start)
string.lastIndexOf(searchvalue,start)
let str = " You come to earth , You have to look at the sun . Walk down the street with your sweetheart , Get to know her , Understand the sun, too "
let result1 = str.indexOf(" The sun ")
let result2 = str.indexOf(" The sun ",10) // from 10 The subscript character starts searching " The sun ", The search scope is " The sun . Walk down the street with your sweetheart , Get to know her , Understand the sun, too ", The subscript is still relative to the original string , Therefore return 11.
let result3 = str.lastIndexOf(" The sun ")
let result4 = str.lastIndexOf(" The sun ",10) // 0-10 The subscript string is " You come to earth , You need to see ", No, " The sun ", return -1
console.log(result1) // Console printing :11
console.log(result2) // Console printing :11
console.log(result3) // Console printing :35
console.log(result4) // Console printing :-1
11. String to case —— toLowerCase() and toUpperCase()
string.toLowerCase()
string.toUpperCase()
let str = "For you, A thousand times over"
let result1 = str.toLowerCase()
let result2 = str.toUpperCase()
console.log(result1) // Console printing :"for you, a thousand times over"
console.log(result2) // Console printing :"FOR YOU, A THOUSAND TIMES OVER"
12. The string is filled to the specified length —— padStart () and padEnd ()
string.padStart(n,' Supplementary content ')
string.padEnd (n,' Supplementary content ')
// Add "-", Until the length of the string is 5
let str1 = ' Ready to start '
let result = str1.padStart(5, '-')
console.log(result) // Console printing :"--- Ready to start "
// Add... At the end "*", Until the length of the string is 11
let str2 = "184"
let result = str2.padEnd(11, "*")
console.log(result) // Console printing :"184********"
13. Whether the string is in a specific character ( strand ) Beginning or end —— startsWith()、endsWith()
startsWith()
endsWith()
startsWith()
endsWith()
startsWith()
endsWith()
string.startsWith(searchvalue, start)
string.endsWith(searchvalue, start)
let str = " In the past , Let's smile at each other , Most of my life has passed "
let result1 = str.startsWith(" In the past ")
let result2 = str.startsWith(" In the past ",10)
let result3 = str.endsWith(" Half my life ")
let result4 = str.endsWith(" Half my life ",20)
console.log(result1); // Console printing :true
console.log(result2); // Console printing :false
console.log(result3); // Console printing :true
console.log(result4); // Console printing :false
14. String length calculation —— length
string.length
let str = " Meet and grow old together , You are quietly bald "
let result = str.length
console.log(result) // Console printing :14
15. String interception —— substr() and slice() and substring()
substr()
slice()
substring()
string.length
let str = '0123456789'
let result1 = str.substr(2,5) // From the subscript 2 Began to intercept , Intercept 5 position
let result2 = str.slice(2,5) // From the subscript 2 Began to intercept , Intercept to subscript 5( Without subscript 5)
let result3 = str.substring(2,5) // From the subscript 2 Began to intercept , Intercept to subscript 5( Without subscript 5)
console.log(result1) // Console printing :23456
console.log(result2) // Console printing :234
console.log(result3) // Console printing :234
summary
边栏推荐
- Advanced technology management - what is the physical, mental and mental strength of managers
- Shell script file traversal STR to array string splicing
- MySQL 巨坑:update 更新慎用影响行数做判断!!!
- COMSOL--三维图形的建立
- In the last process before the use of the risk control model, 80% of children's shoes are trampled here
- 使用GBase 8c数据库过程中报错:80000305,Host ips belong to different cluster ,怎么解决?
- Modulenotfounderror: no module named 'scratch' ultimate solution
- Install esxi 6.0 interactively
- 边缘计算如何与物联网结合在一起?
- COMSOL -- 3D casual painting -- sweeping
猜你喜欢
Idea set the number of open file windows
13. (map data) conversion between Baidu coordinate (bd09), national survey of China coordinate (Mars coordinate, gcj02), and WGS84 coordinate system
Basics - rest style development
高校毕业求职难?“百日千万”网络招聘活动解决你的难题
COMSOL--建立几何模型---二维图形的建立
Harbor image warehouse construction
[crawler] bugs encountered by wasm
简单解决redis cluster中从节点读取不了数据(error) MOVED
How can China Africa diamond accessory stones be inlaid to be safe and beautiful?
13.(地图数据篇)百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换
随机推荐
Unity Xlua MonoProxy Mono代理类
Harbor镜像仓库搭建
Prevent browser backward operation
解决readObjectStart: expect { or n, but found N, error found in #1 byte of ...||..., bigger context ..
2048 game logic
The art of communication III: Listening between people
Empêcher le navigateur de reculer
[crawler] Charles unknown error
使用GBase 8c数据库过程中报错:80000305,Host ips belong to different cluster ,怎么解决?
View all processes of multiple machines
c#操作xml文件
Risc-v-qemu-virt in FreeRTOS_ Scheduling opportunity of GCC
Question and answer 45: application of performance probe monitoring principle node JS probe
NFT 交易市场主要使用 ETH 本位进行交易的局面是如何形成的?
《看完就懂系列》15个方法教你玩转字符串
Intelligent metal detector based on openharmony
Golang application topic - channel
POJ 3176 cow bowling (DP | memory search)
871. Minimum Number of Refueling Stops
居家办公那些事|社区征文