Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

js substring截取字符串

作者:冰残°零度伤   发布日期:2025-07-03   浏览:88

// 示例代码:使用 substring 方法截取字符串

// 定义一个字符串
let str = "Hello, world!";

// 使用 substring 方法从索引 0 到索引 5 截取字符串(不包括索引 5)
let result1 = str.substring(0, 5); // 结果为 "Hello"

// 使用 substring 方法从索引 7 开始截取到字符串结尾
let result2 = str.substring(7); // 结果为 "world!"

// 解释说明:
// substring 方法用于提取字符串中介于两个指定下标之间的部分。
// substring(startIndex, endIndex):
// - startIndex:必需,表示开始截取的索引位置(包含该索引位置的字符)。
// - endIndex:可选,表示结束截取的索引位置(不包含该索引位置的字符)。
// 如果省略 endIndex,则截取从 startIndex 到字符串末尾的所有字符。

console.log(result1); // 输出: Hello
console.log(result2); // 输出: world!

上一篇:js 数组操作

下一篇:js substr和substring的区别

大家都在看

js 数组对象排序

js 数组删掉第一个值

js fill

js 数组复制

js 复制数组

js 数组拷贝

js 对象转数组

js 深拷贝数组

js 点击空白区域触发事件

js 获取今天年月日

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站