当前位置:网站首页>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
边栏推荐
- 简单解决redis cluster中从节点读取不了数据(error) MOVED
- How can edge computing be combined with the Internet of things?
- Home office things community essay
- Golang application topic - channel
- SET XACT_ABORT ON
- Solve the grpc connection problem. Dial succeeds with transientfailure
- IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
- Harbor image warehouse construction
- 跨境电商是啥意思?主要是做什么的?业务模式有哪些?
- COMSOL -- establishment of 3D graphics
猜你喜欢
7.2每日学习4
go语言学习笔记-分析第一个程序
《增长黑客》阅读笔记
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
idea设置打开文件窗口个数
IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
中非 钻石副石怎么镶嵌,才能既安全又好看?
7 themes and 9 technology masters! Dragon Dragon lecture hall hard core live broadcast preview in July, see you tomorrow
11. (map data section) how to download and use OSM data
Intelligent metal detector based on openharmony
随机推荐
Pytorch training process was interrupted
redis 集群模式原理
OneForAll安装使用
ACID事务理论
Web API configuration custom route
Prevent browser backward operation
分类TAB商品流多目标排序模型的演进
Intelligent metal detector based on openharmony
1.php的laravel创建项目
[leetcode] wild card matching
技术分享 | 常见接口协议解析
redis的持久化机制原理
C operation XML file
Harbor image warehouse construction
Cron表达式(七子表达式)
POJ 3176-Cow Bowling(DP||记忆化搜索)
Cdga | six principles that data governance has to adhere to
FreeRTOS 中 RISC-V-Qemu-virt_GCC 的调度时机
百问百答第45期:应用性能探针监测原理-node JS 探针
Ffmpeg calls avformat_ open_ Error -22 returned during input (invalid argument)