JS 中 substr() 方法可提取指定部分的字符串。语法为 substr(start [,length]),其中 start 指定提取起始位置,length 指定提取长度。它可提取部分字符串,从 start 位置开始到字符串末尾或指定长度,不修改原始字符串。

substr() 方法用于提取字符串的指定部分。
<code>substr(start [,length])</code>
提取的子字符串。
提取字符串从指定位置开始的部分:
<code>const str = "Hello World"; console.log(str.substr(6)); // 输出 "World"</code>
提取字符串从指定位置开始指定长度的部分:
<code>const str = "Hello World"; console.log(str.substr(6, 5)); // 输出 "World"</code>
length 参数,则提取从 start 位置到字符串末尾的所有字符。start 为负数,则从字符串末尾开始计数。start 超出字符串长度,则不会提取任何字符。length 为负数,则不提取任何字符。substr() 不会修改原始字符串。以上就是js中substr的用法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号