当前位置:网站首页>《看完就懂系列》15个方法教你玩转字符串
《看完就懂系列》15个方法教你玩转字符串
2022-07-05 11:29:00 【InfoQ】
1. 字符串去除首尾空格 —— trim()
string.trim()
let str = " 白色不白,黑色不黑,我...我不喜欢你 "
let result = str.trim()
console.log(result); // 控制台打印:"白色不白,黑色不黑,我...我不喜欢你"
2. 字符串替换指定字符(串)—— replace()
replace()
string.replace(searchvalue,newvalue)
let str = "你知道吗?世界上有60亿人,宇宙有60万亿小行星,你比小行星还要珍贵一万倍"
let result1 = str.replace("亿","个")
let result2 = str.replace(/亿/g,"个")
console.log(result1) // 控制台打印:"你知道吗?世界上有60个人,宇宙有60万亿小行星,你比小行星还要珍贵一万倍"
console.log(result2) // 控制台打印:"你知道吗?世界上有60个人,宇宙有60万个小行星,你比小行星还要珍贵一万倍"
3. 字符串合并 —— concat()
string.concat()
let str1 = "我有很多好听的情话,"
let str2 = "可面对你都不敢说不出口。"
let result = str1.concat(str2)
console.log(result) // 控制台打印:"我有很多好听的情话,可面对你都不敢说不出口。"
4. 字符串转数组 —— split()
split
string.split()
string.split(/[*]/)
let str = "张三,李四;王五"
let result1 = str.split(',')
let result2 = str.split(/[,;]/)
console.log(result1) // 控制台打印:["张三", "李四;王五"]
console.log(result2) // 控制台打印:["张三", "李四", "王五"]
5. 字符串转数组 —— [...string]
split()
...
[...string]
let str = '这是一个字符串string'
let arr = [...str]
console.log(arr) // 控制台打印:["这", "是", "一", "个", "字", "符", "串", "s", "t", "r", "i", "n", "g"]
6. 字符串反转 —— [...string].reverse().join("")
[...string].reverse().join("")
let str = "两极反转,龙卷风摧毁停车场!"
let result = [...str].reverse().join("")
console.log(result) // 控制台打印:!场车停毁摧风卷龙,转反极两
7. 字符串的多次复制 —— repeat ()
string.repeat(n)
let str1 = '复制'
let result = str1.repeat(2)
consol.log(result) // 控制台打印:复制复制
let str2 = '10'
let result = str2.repeat(5)
console.log(result) // 控制台打印:1010101010
8. 字符串是否包含某字符 (串)—— search()
string.search(searchvalue)
let str = "今天的夜色很好,月亮也很圆,唯一遗憾的是,我不是从你的窗子里看到的月亮。"
let result1 = str.search("月亮")
let result2 = str.search(/[,。]/)
console.log(result1) // 控制台打印:8
console.log(result2) // 控制台打印:7
9. 字符串内是否包含某字符(串)—— includes()
includes()
search()
includes()
search()
includes()
string.includes(searchvalue, start)
let str = "没有夏日的的凉风,也没有冬日的暖阳,它们只是恰好出现在了合适的时间罢了"
let result1 = str.includes("冬日")
let result2 = str.includes("冬日",20)
console.log(result1); // 控制台打印:true
console.log(result2); // 控制台打印:false
10. 字符串中指定的字符串值在首次或最后一次出现的位置 —— indexOf() 和 lastIndexOf()
indexOf()
lastIndexOf()
string.indexOf(searchvalue,start)
string.lastIndexOf(searchvalue,start)
let str = "你来人间一趟,你要看看太阳。和你的心上人一起走在街上,了解她,也要了解太阳"
let result1 = str.indexOf("太阳")
let result2 = str.indexOf("太阳",10) // 从10下标的字符开始查找"太阳",查找范围是"太阳。和你的心上人一起走在街上,了解她,也要了解太阳",下标还是相对原字符串而言的,因此返回11。
let result3 = str.lastIndexOf("太阳")
let result4 = str.lastIndexOf("太阳",10) // 0-10下标的字符串为"你来人间一趟,你要看看",没有"太阳",返回-1
console.log(result1) // 控制台打印:11
console.log(result2) // 控制台打印:11
console.log(result3) // 控制台打印:35
console.log(result4) // 控制台打印:-1
11. 字符串转大小写 —— toLowerCase() 和 toUpperCase()
string.toLowerCase()
string.toUpperCase()
let str = "For you, A thousand times over"
let result1 = str.toLowerCase()
let result2 = str.toUpperCase()
console.log(result1) // 控制台打印:"for you, a thousand times over"
console.log(result2) // 控制台打印:"FOR YOU, A THOUSAND TIMES OVER"
12. 字符串填充到指定长度 —— padStart () 和 padEnd ()
string.padStart(n,'补充内容')
string.padEnd (n,'补充内容')
// 在字符串前补充"-",直到字符串的长度为5
let str1 = '预备开始'
let result = str1.padStart(5, '-')
console.log(result) // 控制台打印:"---预备开始"
//在末尾添加"*",直到字符串的长度为11
let str2 = "184"
let result = str2.padEnd(11, "*")
console.log(result) // 控制台打印:"184********"
13. 字符串是否以特定字符(串)开头或结尾 —— startsWith()、endsWith()
startsWith()
endsWith()
startsWith()
endsWith()
startsWith()
endsWith()
string.startsWith(searchvalue, start)
string.endsWith(searchvalue, start)
let str = "过去的时候,我们相视一笑,就已过了大半辈子"
let result1 = str.startsWith("过去")
let result2 = str.startsWith("过去",10)
let result3 = str.endsWith("半辈子")
let result4 = str.endsWith("半辈子",20)
console.log(result1); // 控制台打印:true
console.log(result2); // 控制台打印:false
console.log(result3); // 控制台打印:true
console.log(result4); // 控制台打印:false
14. 字符串长度计算 —— length
string.length
let str = "相约白头偕老,你却悄悄秃了头"
let result = str.length
console.log(result) // 控制台打印:14
15. 字符串截取 —— substr() 和 slice() 和 substring()
substr()
slice()
substring()
string.length
let str = '0123456789'
let result1 = str.substr(2,5) // 从下标2开始截取,截取5位
let result2 = str.slice(2,5) // 从下标2开始截取,截取到下标5(不含下标5)
let result3 = str.substring(2,5) // 从下标2开始截取,截取到下标5(不含下标5)
console.log(result1) // 控制台打印:23456
console.log(result2) // 控制台打印:234
console.log(result3) // 控制台打印:234
总结
边栏推荐
- Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in
- Summary of websites of app stores / APP markets
- Msfconsole command encyclopedia and instructions
- COMSOL -- 3D casual painting -- sweeping
- ZCMU--1390: 队列问题(1)
- How does redis implement multiple zones?
- Ddrx addressing principle
- 力扣(LeetCode)185. 部门工资前三高的所有员工(2022.07.04)
- C # to obtain the filtered or sorted data of the GridView table in devaexpress
- 解决readObjectStart: expect { or n, but found N, error found in #1 byte of ...||..., bigger context ..
猜你喜欢
[JS] extract the scores in the string, calculate the average score after summarizing, compare with each score, and output
COMSOL -- three-dimensional graphics random drawing -- rotation
【Office】Excel中IF函数的8种用法
pytorch训练进程被中断了
Summary of thread and thread synchronization under window
The ninth Operation Committee meeting of dragon lizard community was successfully held
Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
Characteristics and electrical parameters of DDR4
7 大主题、9 位技术大咖!龙蜥大讲堂7月硬核直播预告抢先看,明天见
7.2每日学习4
随机推荐
Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
【爬虫】wasm遇到的bug
Is it difficult to apply for a job after graduation? "Hundreds of days and tens of millions" online recruitment activities to solve your problems
边缘计算如何与物联网结合在一起?
FFmpeg调用avformat_open_input时返回错误 -22(Invalid argument)
Web API配置自定义路由
R3live series learning (IV) r2live source code reading (2)
我用开天平台做了一个城市防疫政策查询系统【开天aPaaS大作战】
Basics - rest style development
How does redis implement multiple zones?
居家办公那些事|社区征文
COMSOL--三维随便画--扫掠
如何将 DevSecOps 引入企业?
-26374 and -26377 errors during coneroller execution
IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
Three suggestions for purchasing small spacing LED display
Unity xlua monoproxy mono proxy class
管理多个Instagram帐户防关联小技巧大分享
Risc-v-qemu-virt in FreeRTOS_ Scheduling opportunity of GCC
Four departments: from now on to the end of October, carry out the "100 day action" on gas safety