当前位置:网站首页>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()
splitstring.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.lengthlet 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.lengthlet 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
边栏推荐
- View all processes of multiple machines
- Web API configuration custom route
- How did the situation that NFT trading market mainly uses eth standard for trading come into being?
- ZCMU--1390: 队列问题(1)
- 分类TAB商品流多目标排序模型的演进
- COMSOL -- establishment of geometric model -- establishment of two-dimensional graphics
- Three suggestions for purchasing small spacing LED display
- Summary of thread and thread synchronization under window
- 【爬虫】wasm遇到的bug
- [LeetCode] Wildcard Matching 外卡匹配
猜你喜欢

Modulenotfounderror: no module named 'scratch' ultimate solution

Evolution of multi-objective sorting model for classified tab commodity flow

Harbor镜像仓库搭建

13. (map data) conversion between Baidu coordinate (bd09), national survey of China coordinate (Mars coordinate, gcj02), and WGS84 coordinate system

comsol--三维图形随便画----回转

MySQL 巨坑:update 更新慎用影响行数做判断!!!

7 大主题、9 位技术大咖!龙蜥大讲堂7月硬核直播预告抢先看,明天见

无密码身份验证如何保障用户隐私安全?

OneForAll安装使用

Go language learning notes - analyze the first program
随机推荐
Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
spark调优(一):从hql转向代码
idea设置打开文件窗口个数
COMSOL -- 3D casual painting -- sweeping
I used Kaitian platform to build an urban epidemic prevention policy inquiry system [Kaitian apaas battle]
Ziguang zhanrui's first 5g R17 IOT NTN satellite in the world has been measured on the Internet of things
What does cross-border e-commerce mean? What do you mainly do? What are the business models?
redis的持久化机制原理
PHP中Array的hash函数实现
SET XACT_ ABORT ON
COMSOL -- establishment of geometric model -- establishment of two-dimensional graphics
龙蜥社区第九次运营委员会会议顺利召开
871. Minimum Number of Refueling Stops
XML parsing
TSQL – identity column, guid, sequence
Empêcher le navigateur de reculer
[crawler] Charles unknown error
ZCMU--1390: 队列问题(1)
【爬虫】charles unknown错误
MFC pet store information management system