当前位置:网站首页>JS operation string string string
JS operation string string string
2022-07-19 12:50:00 【Xuan Yu Shang】

Catalog
One . Access string characters
Four . toUpperCase : Make all characters uppercase
5、 ... and . toLowerCase : Change all characters to lowercase
1 - indexOf ( searchString , fromIndex ) : lookup
2 - includes( searchString , position ) : lookup
3 - startsWith : With xxx start
4 - endsWith : With xxx ending
7、 ... and . replace : Replace string
Ten . trim : Delete the first and last strings
11、 ... and . split( swparator,limit ) : String segmentation
String - JavaScript | MDN
One . Access string characters
const message = 'hello world'
console.log( message[1] ) // e
console.log( message.charAt(4) ) // oTwo . String traversal
1 - Normal cycle
const message = 'hello world'
for (let i = 0; i < message.length; i++) {
console.log(message[i])
}2 - for...of loop
// for..of The traversal -> iterator
// Currently iteratible objects : character string / Array
// Object is not supported for..of
// String Inside the object is to turn the string into an iteratable object
for (let char of message) {
console.log(char)
}3、 ... and . Modify string
After the string definition is Cannot be modified Of , So when directly manipulating characters is invalid
let message = "Hello World"
message[2] = "A"
console.log(message) // Hello World. It won't change Four . toUpperCase : Make all characters uppercase
const message = 'hello'
console.log(message.toUpperCase()) // HELLO , The original string has not changed , Instead, a new string is generated 5、 ... and . toLowerCase : Change all characters to lowercase
const message = 'HeLLo'
console.log(message.toUpperCase()) // hello, The original string has not changed , Instead, a new string is generated 6、 ... and . Find string
1 - indexOf ( searchString , fromIndex ) : lookup
- Situation 1 : Search for , The index position of the search string
- Situation two : No search for , return -1
const message = 'my name is coder'
let index = message.indexOf('name')
if(index !== -1){
console.log(' eureka ')
}else{
console.log(' Did not find ')
}2 - includes( searchString , position ) : lookup
const message = 'my name is coder'
if(message.includes('name')){
console.log(' eureka ')
}3 - startsWith : With xxx start
const message = 'my name is coder'
if (message.startsWith("my")) {
console.log("message With my start ")
}4 - endsWith : With xxx ending
const message = 'my name is coder'
if (message.endsWith("coder")) {
console.log("message With my ending ")
}7、 ... and . replace : Replace string
- Find the corresponding string , And use the new string to replace
- You can also pass in a regular expression to find , You can also pass in a function to replace
// Incoming string
const message = 'my name is star'
let newMessage = message.replace("star", "kobe")
console.log(newMessage) // my name is kobe
// Passing in functions
const newName = "kobe"
let newMessage = message.replace("star", function() {
return newName.toUpperCase()
})
console.log(newMessage) // my name is KOBE8、 ... and . Get substring

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 WorlNine . String concatenation
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"
// Chainable call
const newString2 = str1.concat(str2).concat(str3)
// Multiple values can be passed in at the same time
const newString3 = str1.concat(str2, str3, "abc", "cba")Ten . trim : Delete the first and last strings
console.log(" star abc ".trim()) // star abc11、 ... and . split( swparator,limit ) : String segmentation
- separator: What string to split , It can also be a regular expression ;
- limit: Limit the number of returned fragments ;
const message = "abc-cba-nba-mba"
// The cutting character is => -
const items = message.split("-")
console.log(items) // ['abc','cba','nba','mba']
// By array join Method , Variable to string The connecting character is *
const newMessage = items.join("*")
console.log(newMessage) // abc*cba*nba*mba
//-----------------------------------------------
const message = 'abc-cba-nba-mba';
// Return length is 2 Array of
const items = message.split('-', 2);
console.log(items); // ['abc','cba']边栏推荐
- The active and standby cache of redis cluster is full, causing frequent active and standby switchover
- Review the 2008 financial crisis and become a long-term investor 2020-03-19
- 超声波传感器系列文章汇总
- 逻辑运算符1(阁瑞钛伦特软件-九耶实训)
- 运维小白成长记—架构第6周
- July training (day 17) - breadth first search
- Hcip second day notes
- O & M LITTLE WHITE Growth record - architecture week 6
- MyCat2搭建mysql主从分离
- 懒到骨子里了,我在CSDN写文章都懒得自己写了,基于selenium模拟写文章
猜你喜欢

第四天作業

通货收缩的市场何时反转?我们该如何操作?2020-03-13

The leader of the new generation of cloud database -- AWS

Talk about the redis cache penetration scenario and the corresponding solutions

音频常见端子剖析图---再也不会搞错了

超声波传感器(CH101&ch201) - Ⅰ

String correlation function (II)

动态内存规划

Ultrasonic sensor (ch101 & ch201) - Ⅱ

Machine learning Assignment 1
随机推荐
Acwing4405. 统计子矩阵
2022全球开发者薪资曝光:中国排第19名,平均年薪23,790美元
ASP. Net collaborative OA office service management platform source code
Lazy to the bone, I am too lazy to write articles in CSDN. I write articles based on selenium simulation
音频控制常见BUG注意事项
Review the 2008 financial crisis and become a long-term investor 2020-03-19
Ah Qu's thinking
Array de duplication array sorting maximum sum class array conversion filter array
数组去重 数组排序 最大值 求和 类数组转化 过滤数组
运维小白成长记—架构第6周
Competition notes: numpy learning notes
全球金融危机来袭,如何科学理性投资?2020-03-17
第四天作業
Will webgpu become the killer of webgl?
RingBuffer
Redis集群主备缓存区满了导致主备频繁倒换
Arbitrum Nova release! Create a low-cost and high-speed dedicated chain in the game social field
LeetCode_ Prefix and_ Medium_ 523. Continuous subarrays and
Which domestic API tool is better? It turned out to be it
Return to risk ratio: the most important indicator of investment opportunities 2020-03-14