当前位置:网站首页>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
边栏推荐
- AutoCAD -- mask command, how to use CAD to locally enlarge drawings
- 解决grpc连接问题Dial成功状态为TransientFailure
- ZCMU--1390: 队列问题(1)
- Manage multiple instagram accounts and share anti Association tips
- 以交互方式安装ESXi 6.0
- Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
- Leetcode 185 All employees with the top three highest wages in the Department (July 4, 2022)
- Cron表达式(七子表达式)
- ibatis的动态sql
- Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in
猜你喜欢
12. (map data) cesium city building map
CDGA|数据治理不得不坚持的六个原则
Stop saying that microservices can solve all problems!
COMSOL -- establishment of 3D graphics
7 大主题、9 位技术大咖!龙蜥大讲堂7月硬核直播预告抢先看,明天见
[office] eight usages of if function in Excel
11.(地图数据篇)OSM数据如何下载使用
AUTOCAD——遮罩命令、如何使用CAD对图纸进行局部放大
13.(地图数据篇)百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换
COMSOL -- 3D casual painting -- sweeping
随机推荐
【爬虫】wasm遇到的bug
POJ 3176-Cow Bowling(DP||记忆化搜索)
How can edge computing be combined with the Internet of things?
871. Minimum Number of Refueling Stops
shell脚本文件遍历 str转数组 字符串拼接
OneForAll安装使用
Is it difficult to apply for a job after graduation? "Hundreds of days and tens of millions" online recruitment activities to solve your problems
Modulenotfounderror: no module named 'scratch' ultimate solution
Dspic33ep clock initialization program
Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in
SLAM 01. Modeling of human recognition Environment & path
FreeRTOS 中 RISC-V-Qemu-virt_GCC 的调度时机
FFmpeg调用avformat_open_input时返回错误 -22(Invalid argument)
COMSOL--三维图形的建立
Cron表达式(七子表达式)
POJ 3176 cow bowling (DP | memory search)
分类TAB商品流多目标排序模型的演进
DDoS attack principle, the phenomenon of being attacked by DDoS
Manage multiple instagram accounts and share anti Association tips
石油化工企业安全生产智能化管控系统平台建设思考和建议