当前位置:网站首页>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
边栏推荐
- Go language learning notes - analyze the first program
- What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
- IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
- Project summary notes series wstax kt session2 code analysis
- sklearn模型整理
- Solve the problem of slow access to foreign public static resources
- COMSOL--三维随便画--扫掠
- FFmpeg调用avformat_open_input时返回错误 -22(Invalid argument)
- 12.(地图数据篇)cesium城市建筑物贴图
- C operation XML file
猜你喜欢

Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?

COMSOL--建立几何模型---二维图形的建立
![[crawler] bugs encountered by wasm](/img/29/6782bda4c149b7b2b334238936e211.png)
[crawler] bugs encountered by wasm

Pytorch training process was interrupted

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

【爬虫】charles unknown错误

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

11. (map data section) how to download and use OSM data

Advanced technology management - what is the physical, mental and mental strength of managers

Idea set the number of open file windows
随机推荐
Manage multiple instagram accounts and share anti Association tips
Dspic33ep clock initialization program
MFC pet store information management system
CDGA|数据治理不得不坚持的六个原则
What about SSL certificate errors? Solutions to common SSL certificate errors in browsers
Three suggestions for purchasing small spacing LED display
技术分享 | 常见接口协议解析
12.(地图数据篇)cesium城市建筑物贴图
COMSOL--三维图形的建立
[crawler] bugs encountered by wasm
Advanced technology management - what is the physical, mental and mental strength of managers
ibatis的动态sql
基于Lucene3.5.0怎样从TokenStream获得Token
MySQL 巨坑:update 更新慎用影响行数做判断!!!
查看多台机器所有进程
idea设置打开文件窗口个数
uboot的启动流程:
COMSOL--三维随便画--扫掠
COMSOL -- 3D casual painting -- sweeping
SET XACT_ ABORT ON