当前位置:网站首页>JS 之 操作 String 字符串
JS 之 操作 String 字符串
2022-07-17 10:57:00 【玄鱼殇】

目录
1 - indexOf ( searchString , fromIndex ) : 查找
2 - includes( searchString , position ) : 查找
十一. split( swparator,limit ) : 字符串分割
String - JavaScript | MDN
一. 访问字符串字符
const message = 'hello world'
console.log( message[1] ) // e
console.log( message.charAt(4) ) // o二. 字符串遍历
1 - 普通循环
const message = 'hello world'
for (let i = 0; i < message.length; i++) {
console.log(message[i])
}2 - for...of循环
// for..of的遍历 -> 迭代器
// 目前可迭代对象: 字符串/数组
// 对象是不支持for..of
// String对象内部是将字符串变成了一个可迭代对象
for (let char of message) {
console.log(char)
}三. 修改字符串
当字符串定义后是不可以修改的,所以当直接操作字符是无效的
let message = "Hello World"
message[2] = "A"
console.log(message) // Hello World. 不会更改的四. toUpperCase : 将所有字符变为大写
const message = 'hello'
console.log(message.toUpperCase()) // HELLO ,原来的字符串没有更改,而是生成了新的字符串五. toLowerCase : 将所有字符变为小写
const message = 'HeLLo'
console.log(message.toUpperCase()) // hello,原来的字符串没有更改,而是生成了新的字符串六. 查找字符串
1 - indexOf ( searchString , fromIndex ) : 查找
- 情况一: 搜索到, 搜索字符串所在索引位置
- 情况二: 没有搜索到, 返回-1
const message = 'my name is coder'
let index = message.indexOf('name')
if(index !== -1){
console.log('找到了')
}else{
console.log('没找到')
}2 - includes( searchString , position ) : 查找
const message = 'my name is coder'
if(message.includes('name')){
console.log('找到了')
}3 - startsWith : 以xxx开头
const message = 'my name is coder'
if (message.startsWith("my")) {
console.log("message以my开头")
}4 - endsWith : 以xxx结尾
const message = 'my name is coder'
if (message.endsWith("coder")) {
console.log("message以my结尾")
}七. replace : 替换字符串
- 查找到对应的字符串,并且使用新的字符串进行替代
- 也可以传入一个正则表达式来查找,也可以传入一个函数来替换
// 传入字符串
const message = 'my name is star'
let newMessage = message.replace("star", "kobe")
console.log(newMessage) // my name is kobe
// 传入函数
const newName = "kobe"
let newMessage = message.replace("star", function() {
return newName.toUpperCase()
})
console.log(newMessage) // my name is KOBE八. 获取子字符串

const message = "Hello World"
console.log(message.slice(3, 7)) // lo W
console.log(message.slice(3, -1)) // lo Worl
console.log(message.slice(3)) // lo World
console.log(message.substr(3, 7)) // lo Worl九. 拼接字符串
1 - +
const str1 = "Hello"
const str2 = "World"
const str3 = "kobe"
const newString = str1 + str2 + str3
console.log(newString)2 - concat
const str1 = "Hello"
const str2 = "World"
const str3 = "kobe"
// 可链式调用
const newString2 = str1.concat(str2).concat(str3)
// 可同时传入多个值
const newString3 = str1.concat(str2, str3, "abc", "cba")十. trim : 删除首尾字符串
console.log(" star abc ".trim()) // star abc十一. split( swparator,limit ) : 字符串分割
- separator:以什么字符串进行分割,也可以是一个正则表达式;
- limit:限制返回片段的数量;
const message = "abc-cba-nba-mba"
// 切割字符为 => -
const items = message.split("-")
console.log(items) // ['abc','cba','nba','mba']
// 通过数组的join方法,可变为字符串 连接字符为 *
const newMessage = items.join("*")
console.log(newMessage) // abc*cba*nba*mba
//-----------------------------------------------
const message = 'abc-cba-nba-mba';
// 返回长度为2的数组
const items = message.split('-', 2);
console.log(items); // ['abc','cba']边栏推荐
- 钴铁双金属有机骨架Cox/MIL-100(Fe)|光敏剂@[email protected]|负载CQDs的FeMIL101材料|mof试剂
- Go-Excelize API源码阅读(二)——OpenFile()
- [C language] void type and void* pointer type
- 将视频格式转换为gif图片格式
- Part I - Fundamentals of C language_ 3. Operators and expressions
- Mysqldump full recovery to another new instance, and then perform flush privileges analysis
- C51 常见数据类型详解
- NPM usage
- [C language] Pointer exercise 2 - real written test questions and analysis
- 数据库概述
猜你喜欢

金纳米粒子修饰MIL-101骨架材料(AuNPs/MIL-101)/负载COF-TpPa-1(Au NPs/COF-TpPa-1)|齐岳试剂

Part I - Fundamentals of C language_ 5. Arrays and strings

Implement word segmentation for text and draw word cloud

第一部分—C语言基础篇_6. 函数

Part I - Fundamentals of C language_ 1. Overview of C language

壳聚糖包裹PCN224纳米粒子|金属-有机骨架Fe-MIL-88NH2|镍基MOF材料(Ni-MOF/NF)

Experiment 1: camera calibration experiment using Matlab toolbox

【洛谷】P2357 守墓人
![[hero planet July training leetcode problem solving daily] 17th kuansou](/img/92/9b8e3a710430d37564d7ea3f28168f.png)
[hero planet July training leetcode problem solving daily] 17th kuansou

C51 常见数据类型详解
随机推荐
R language data Table import data practice: data Table uses dcast data. The table function implements pivot table
D. Mark and Lightbulbs
[C language] data type and meaning
CLWY权限管理(三)--- 用户组模块
第一部分—C语言基础篇_5. 数组和字符串
sqli-labs(less-11)
数据库概述
第4章-一阶多智体系统一致性 -> 连续时间含时延系统一致性
面试题-给::memcpy函数设计测试用例
Database overview
第九章 STL 之 deque
v-mode
Why is SaaS so important for enterprise digital transformation?
565. 数组嵌套 / 剑指 Offer II 001. 整数除法
Mysqldump full recovery to another new instance, and then perform flush privileges analysis
【C语言】函数知识点总结
es索引、类型(mapping)、文档、ik分词器
Rhcsa the next day 7.15
Have you learned the database design pattern of multi tenant SaaS?
Utility series - xshell installation, download and use