当前位置:网站首页>JS string interception method summary
JS string interception method summary
2022-06-30 19:16:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
One 、 Use slice() Intercept
1, Function description
slice() Method by specifying the start and end positions , Extract a part of the string , And return the extracted part with a new string .
The grammar is as follows :
string.slice(start, end); start( It's necessary ): Specify where to start selecting . If it's a negative number , Then it specifies the position from the end of the string . in other words ,-1 Refers to the last character ,-2 Refers to the penultimate character , And so on . Parameter description :
end( Optional ): Specify where to end the selection , That is, the character subscript at the end . If the parameter is not specified , Then the intercepted string contains from start All characters to the end . If this parameter is negative , Then it specifies the characters starting from the end of the array .
2, Use samples
var str = “0123456789”;
console.log(“ Original string :”, str);
console.log(“ From index to 3 From character to end :”, str.slice(3)); //3456789
console.log(“ From the bottom of the first 3 From character to character :”, str.slice(-3)); //789
console.log(“ From the beginning until the index is 5 The previous character of :”, str.slice(0,5)); //01234
console.log(“ From the beginning to the penultimate 3 The first character of a character :”, str.slice(0,-3)); //0123456
console.log(“ From index to 3 The character of is indexed as 5 The previous character of :”, str.slice(3,5)); //34
console.log(“ From index to 3 The character of starts from the penultimate 3 The first character of a character :”, str.slice(3,-3)); //3456
Two 、 Use substring() Intercept
1, Function description
(1)substring Method is used to extract characters that are mediated between two specified subscripts .
The grammar is as follows :
string.substring(start, stop)
start( It's necessary ): A nonnegative integer , Specifies that the first character of the substring to be extracted is in stringObject Position in . Parameter description :
stop( Optional ): A nonnegative integer , The last character of the substring to be extracted is in stringObject There are many places in 1.
Return value description : This method returns a new string , The string value contains stringObject A substring of , Its content is from start Go to stop-1 All characters at , The length of stop reduce start.
(2) matters needing attention :
- If start And end equal , Then the method returns an empty string ( The length is 0 String ).
- If start Than end Big , Then the method will exchange these two parameters before extracting the substring .
- If start or end It's a negative number , It will be replaced by 0.
2, Use samples
var str = “0123456789”;
console.log(“ Original string :”, str);
console.log(“ From index to 3 From character to end :”, str.substring(3)); //3456789
console.log(“ From index to 20 From character to end :”, str.substring(20)); //
console.log(“ From index to 3 The character of is indexed as 5 The previous character of the ends :”, str.substring(3,5)); //34
console.log(“start Than end Conference automatic exchange , The same as above :”, str.substring(5,3)); //34
console.log(“ From index to 3 The character of is indexed as 20 The previous character of the ends :”, str.substring(3,20)); //3456789
3、 ... and 、 Use substr() Intercept
1, Function description
substr Method is used to return a substring of a specified length starting at a specified position .
The grammar is as follows :
stringObject.substr(start, length); start( It's necessary ): The starting position of the required substring . The index of the first character in the string is 0. length( Optional ): The number of characters that should be included in the returned substring .
(2) matters needing attention :
- If length by 0 Or negative numbers , Will return an empty string .
- If not specified length, Then the substring will continue to stringObject Last .
- If start or length It's a negative number , It will be replaced by 0.
2, Use samples
var str = “0123456789”;
console.log(“ Original string :”, str);
console.log(“ From index to 3 From character to end :”, str.substr(3)); //3456789
console.log(“ From index to 20 From character to end :”, str.substr(20)); //
console.log(“ From index to 3 The intercept length from the character of is 5 String :”, str.substr(3,5)); //34567
console.log(“ From index to 3 The intercept length from the character of is 20 String :”, str.substr(3,20)); //3456789
Four 、 Other common methods
1,indexOf()
Returns the subscript of the first character in a string that matches a substring .
var str = “JavaScript”;
var i1 = str.indexOf(“a”); //1
var i2 = str.indexOf(“S”); //4
var i3 = str.indexOf(“Script”); //4
var i4 = str.indexOf(“k”); //-1
console.log(“ Original string :”, str);
console.log(“a The index of :”, i1);
console.log(“S The index of :”,i2);
console.log(“Script The index of :”,i3);
console.log(“k The index of :”,i4);
2,lastIndexOf()
This method returns the index value of the first character of a character or string from right to left ( And indexOf contrary )
var str = “JavaScript”;
var i1 = str.lastIndexOf(“a”); //3
var i2 = str.lastIndexOf(“S”); //4
var i3 = str.lastIndexOf(“Script”); //4
var i4 = str.lastIndexOf(“k”); //-1
3,split()
Use a specified separator to partition a string into an array .
var str = “jpg|bmp|gif|ico|png”;
var arr= str.split(“|”);
console.log(arr);
4,join()
Combines an array into a string using a specified delimiter .
var arr= new Array(“jpg”,”bmp”,”gif”,”ico”,”png”);
var str = arr.join(“|”);
console.log(str);
5,concat()
Join two arrays together .
var arr1 = [1, 2, 3];
var arr2 = [4, 5, 6];
var arr3 = arr1.concat(arr2);
console.log(arr3);
6,charAt()
Returns the character in the specified position . The subscript of the first character in the string is 0. If parameters index be not in 0 And string.length Between , This method will return an empty string .
var str = “abcdefg”;
var char = str.charAt(2); //c
console.log(char);
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/132247.html Link to the original text :https://javaforall.cn
边栏推荐
- 简述机器学习中的特征工程
- NBI visual platform quick start tutorial (V) introduction to editor functions and operations
- 领导:谁再用 Redis 过期监听实现关闭订单,立马滚蛋!
- Kalman滤波器--从高斯融合推导
- Detailed single case mode
- CTF流量分析常见题型(二)-USB流量
- RFFE中MIPI协议
- slice
- 挖财账号开户安全吗?是靠谱的吗?
- Construction and practice of full stack code test coverage and use case discovery system
猜你喜欢

sqlserver SQL Server Management Studio和Transact-SQL创建账户、创建访问指定数据库的只读用户

全栈代码测试覆盖率及用例发现系统的建设和实践

Huaxing Securities: kitex practice under the original hybrid Cloud Architecture

Swin-transformer --relative positional Bias

Detailed single case mode

联想YOGA 27 2022,超强配置全面升级

【合集- 行业解决方案】如何搭建高性能的数据加速与数据编排平台

Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
![[Collection - industry solutions] how to build a high-performance data acceleration and data editing platform](/img/56/9f3370eac60df182971607aa642dc2.jpg)
[Collection - industry solutions] how to build a high-performance data acceleration and data editing platform

The cloud native landing practice of using rainbow for Tuowei information
随机推荐
Kalman滤波器--从高斯融合推导
《所谓情商高,就是会说话》读书笔记
German agbb VOC hazardous substances test
torch stack() meshgrid()
Swin-Transformer(2021-08)
Adhering to the concept of 'home in China', 2022 BMW children's traffic safety training camp was launched
mysql修改数据类型_MySQL修改字段类型[通俗易懂]
新版EasyGBS如何配置WebRTC视频流格式播放?
Vs common shortcut key commands
ANSI/UL 94 5-V级垂直燃烧试验
TCP粘包问题
BeanUtils.copyProperties() 对比 mapstruct
com.alibaba.fastjson.JSONObject # toJSONString 消除循环引用
Ansi/ul 94 class 5-V vertical combustion test
Pytorch learning (III)
简述机器学习中的特征工程
Ditto设置全局仅粘贴文本快捷键
Redis beginner to master 01
Ambient light and micro distance detection system based on stm32f1
dtd建模