js字符串方法总结

2. 字符串分割

#### split() 用于分割字符串,执行后返回一个数组

- 参数1:是根据指定内容进行截断,并且该内容会去除

- 参数2:指定返回数组的长度,可选。若指定长度小于等于实际分割长度,则按分割结果从索引值 0 位置截取指定长度数组返回;若指定长度超出实际分割长度,则返回实际分割数组,不会额外加长。

 var str = "I, am ,a ,good ,student"

        var result = null;

        result = str.split(',');//(5) ['I', ' am ', 'a ', 'good ', 'student']

        console.log(result);

        result = str.split(',', 3);

        console.log(result);//) ['I', ' am ', 'a ']

        result = str.split(',', 13);

        console.log(result); // ['I', ' am ', 'a ', 'good ', 'student']

        result = str.split('');//值为空。截取所有字符

        console.log(result);/  /['I', ',', ' ', 'a', 'm', ' ', ',', 'a', ' ', ',', 'g', 'o', 'o', 'd', ' ', ',', 's', 't', 'u', 'd', 'e', 'n', 't']

        // 案例 1. 找出一个语句中最长的单词        

        var str1 = "I ,come ,from ,china,  I ,am ,is, a ,hansome ,man "

        var max = '';

        str1.split(',').forEach((word) => {

            if (word.length > max.length) {

                max = word

            }

            return max;

        })

        console.log(max);//hansome

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值