JavaScript substr() 字符串截取函数使用详解
时间:2024-06-05 09:55:13
substr 定义和用法
substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符。
语法
stringObject.substr(start,length)
参数 | 描述 |
---|---|
start | 必需。要抽取的子串的起始下标。必须是数值。如果是负数,那么该参数声明从字符串的尾部开始算起的位置。也就是说,-1 指字符串中最后一个字符,-2 指倒数第二个字符,以此类推。 |
length | 可选。子串中的字符数。必须是数值。如果省略了该参数,那么返回从 stringObject 的开始位置到结尾的字串。 |
说明
如果 length 为 0 或负数,将返回一个空字符串。如果没有指定该参数,则子字符串将延续到 stringvar 的最后。
示例
下面的示例演示了substr 方法的用法。
第一个截取字符串第一个字符.
<script type="text/javascript"> var str="jb51"; alert(str.substr(0,1)); </script>
结果为: j
<script type="text/javascript"> function SubstrDemo(){ var s, ss; // 声明变量。 var s = "The rain in Spain falls mainly in the plain."; ss = s.substr(12, 5); // 获取子字符串。 return(ss); // 返回 "Spain"。 } alert(SubstrDemo()); </script>
意思就是截取字符串中从第12个字符开始,5个长度
在本例中,我们将使用 substr() 从字符串中提取一些字符:
<script type="text/javascript">var str="Hello world!"document.write(str.substr(3))</script>
输出:
lo world!
例子 2
在本例中,我们将使用 substr() 从字符串中提取一些字符:
<script type="text/javascript">var str="Hello world!"document.write(str.substr(3,7))</script>
输出:
lo worl
标签:substr,字符串截取
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
远程登陆SQL Server 2014数据库的方法
2024-01-28 03:39:01
![](https://img.aspxhome.com/file/2023/0/99990_0s.png)
PHP 正则表达式常用函数使用小结
2024-05-03 15:35:15
mysql本地登录无法使用端口号登录的解决方法
2024-01-25 06:51:30
python实现从尾到头打印单链表操作示例
2021-12-20 00:09:32
pygame+opencv实现读取视频帧的方法示例
2021-01-04 23:41:27
混淆矩阵Confusion Matrix概念分析翻译
2022-07-05 15:08:56
![](https://img.aspxhome.com/file/2023/7/107167_0s.png)
Django中提供的6种缓存方式详解
2023-03-24 14:55:49
MySQL系列之九 mysql查询缓存及索引
2024-01-22 12:46:10
![](https://img.aspxhome.com/file/2023/6/124536_0s.png)
Python 循环读取数据内存不足的解决方案
2022-02-25 17:32:17
![](https://img.aspxhome.com/file/2023/1/80771_0s.jpg)
pandas中对文本类型数据的处理小结
2022-06-25 06:39:16
![](https://img.aspxhome.com/file/2023/7/70787_0s.png)
python Django框架实现web端分页呈现数据
2022-07-03 03:47:14
Python 格式化输出_String Formatting_控制小数点位数的实例详解
2023-11-29 12:09:14
PyQt5使用mimeData实现拖拽事件教程示例解析下
2021-02-15 11:09:03
![](https://img.aspxhome.com/file/2023/2/97642_0s.gif)
OpenSearch 初探
2008-06-19 12:06:00
![](https://img.aspxhome.com/file/UploadPic/20086/19/2008619121131785s.png)
Python面向对象程序设计中类的定义、实例化、封装及私有变量/方法详解
2021-10-15 16:14:07
Python采集猫眼两万条数据 对《无名之辈》影评进行分析
2021-07-20 19:04:38
![](https://img.aspxhome.com/file/2023/3/131723_0s.png)
python科学计算之numpy——ufunc函数用法
2023-05-13 15:13:12
![](https://img.aspxhome.com/file/2023/8/70828_0s.jpg)
图片自动更新(说明)
2024-04-28 09:46:51
Python正则表达式匹配ip地址实例
2023-06-13 15:06:12
python中setuptools的作用是什么
2022-01-31 07:19:26