当前位置:网站首页>JS intercept string (3 methods)
JS intercept string (3 methods)
2022-07-20 23:37:00 【Short and brilliant】
String method | explain |
slice() | Extract a substring |
substr() | Extract a substring |
substring() | Returns a substring of a string |
String Type intercepting string method
Intercepts a string of specified length
substr() Method can intercept the substring according to the specified length . It has two parameters , The first parameter represents the starting subscript of the substring to be intercepted , The second parameter represents the length of the interception .
Example 1
Use... In the following example lastIndexOf() Gets the last position of the subscript in the string , Then intercept from the following position 4 Characters .
var s = "http://c.biancheng.net/index.html";
var b = s.substr(s.lastIndexOf(".") + 1,4); // After intercepting the last dot 4 Characters
console.log(b); // Returns a substring “html”
If you omit the second parameter , It means to intercept all characters from the beginning to the end . Considering that the length of the extension is not fixed , Omitting the second parameter is more flexible .
var b = s.substr(s.lastIndexOf(".") + 1);
If the first parameter is negative , Indicates that the subscript position is calculated from the end of the string , namely -1 Represents the last character ,-2 Represents the penultimate character , And so on . This is useful when the left character length is not fixed .
ECMAScript This method is no longer recommended , Recommended slice() and substring() Method .
Intercept the start and end subscript position string
slice() and substring() Methods intercept the string according to the specified start and end subscript position , They can all contain two parameters , The first parameter represents the starting subscript , The second parameter represents the end subscript .
Example 2
The following code uses substring() Method interception URL Web site host name information in string .
var s = "http://c.biancheng.net/index.html";
var a = s.indexOf("c");
var b = s.indexOf("/", a);
var c = s.substring(a,b);
var d = s.slice(a,b);
- The intercepted string contains the character specified by the first parameter . The end point is not intercepted , That is, it is not included in the string .
- If the second parameter is omitted , Represents all strings intercepted to the end .
If the value of the first parameter is greater than the value of the second parameter ,substring() Method can exchange two parameters before performing interception , And for slice() methods , Is ignored as invalid , And return an empty string .
Example 3
The following code compares substring() Methods and slice() Different methods and usages .
var s = "http://c.biancheng.net/index.html";
var a = s.indexOf("c");
var b = s.indexOf("/", a);
var c = s.substring(b, a);
var d = s.slice(b, a);
- When the values of the start and end points cannot be determined , Use substring() More appropriate .
- If the parameter value is negative ,slice() Method can interpret the minus sign as positioning from the right , This is related to Array Of slice() In the same way . however substring() Method will treat it as invalid , And return an empty string .
Example 4
The following code compares substring() Methods and slice() Methods are used differently .
var s = "http://c.biancheng.net/index.html";
var a = s.indexOf("c");
var b = s.indexOf("/", a);
var 1 = s.length;
var c = s.substring(a-1, b-1);
var d = s.slice(a-1, b-1);
边栏推荐
- 【软考软件评测师】2015综合知识历年真题
- Digitalization has not gone far, and the era of digital intelligence has come
- Envoy monitoring management
- 【Flink】转换算子 map
- display属性
- js截取字符串(3种方法)
- bond网卡模式设置
- [swintransformer source code reading 1] build_ Loader part code
- 教你使用CANN将照片一键转换成卡通风格
- Localization Distillation for Dense Object Detection(用于密集目标检测的定位蒸馏)CVPR2022
猜你喜欢
随机推荐
「接口测试入门课」打卡学习 day04:如何把流程化的测试脚本抽象为测试框架?
A combination idea of developing applet +app) with fluent
The listing of coinbase will open a bull market, rising in the second half 2021-04-03
【Flink】转换算子 map
High number_ Chapter 2 differential calculus of multivariate functions__ Method of solving conditional extremum__ Lagrange multiplier method
数仓:金融级数仓架构转型的最佳实践
企业数字化转型之传统架构到中台微服务架构
MoveIt2——10.URDF与SRDF
给1万帧视频做目标分割,显存占用还不到1.4GB,代码已开源 | ECCV 2022
簡單斐波那契
力扣第三题
数据库之sqlite3 常用操作
【文件上传】解析文本文件通过JDBC连接进行批处理入库(动态建表动态入库)
Envoy监听管理
浅析IM即时通讯开发之扫码登录二维码
Puzzling switch
. Net6 miniapi (4): Configuration
300.最长递增子序列
bond网卡模式设置
Analysis of QR code scanning and login in IM instant messaging development