String对象

javascript中对象分三种:自定义对象,内置对象,浏览器对象

 1) indexOf 查找子字符串第一次出现的位置

 str.indexOf(查找的内容,开始查找的位置)
        注意:字符串下表是从0开始的
        查找不到返回-1
        var str = "sdfhjkdsfhjsdhf";
        console.log(str.indexOf('f'));
        console.log(str.indexOf('f',3));
        console.log(str.indexOf('f',9));
        console.log(str.indexOf('f',15));
         
        // 案例:在字符串中查找f出现的位置和次数    
        var str = "sdfhjkdsfhjsdhf";
        var index = str.indexOf('f');
        var num = 0;
        while(index != -1){
            num++;
            console.log("下标为:",index);
            index = str.indexOf('f',index+1);
        }
        console.log("f一共出现了",num,"次");
        
        // 封装方法:查找某个字符在字符串中出现的次数和位置
        function find(str,findStr){
            var index = str.indexOf(findStr);
            var num = 0;
            while(index != -1){
                num++;
                console.log("下标为:",index);
                index = str.indexOf(findStr,index+1);
            }
            console.log(findStr+"一共出现了",num,"次");
        }
        find('euweiuouriowe','e');

2. lastIndexOf(查找的内容,start) 查找子字符串最后一次出现的位置

 var str = "sdfhjkdsfhjsdhf";
        console.log(str.lastIndexOf('f'));//14

3. slice(start,end) 获取字符串的某个部分 截取是前包含,后不包含

        var str1= 'hello world';
        console.log(str1.slice(6));// world
        console.log(str1.slice(6,9));// wor
        console.log(str1.slice(6,-1));// worl

4. 截取 substr(起始位置,[截取长度])   //截取不写则代 表截取到字符串未尾

        截取长度:可以是一个数字,还可以不写;如果不写长度,表示截取到字符串的末尾

        var str1= 'hello world';
        console.log(str1.substr(6,5));

 5.  replace('子字符串1'string/正则,'子字符串2') 替换

        var str='My name is apple. So I like to eat apple very much!';
         var newstr = str.replace('apple','苹果'); // return
        console.log(newstr);//My name is 苹果. So I like to eat apple very much!
        
         正则表达式; g表示global,全局的意思
        var newstr = str.replace(/apple/g,'苹果'); 
        console.log(newstr);//My name is 苹果. So I like to eat 苹果 very much!
        var str2 = '张三7李四8王五3马六a马六m马六';
        // console.log(str2.replace('1',',').replace('2',',').replace('3',','));
        console.log(str2.replace(/1|2|3/g,',')); // 1或者2或者3
        console.log(str2.replace(/[0123456789]/g,',')); // 所有数字
        console.log(str2.replace(/[0-9]/g,',')); // 所有数字
        console.log(str2.replace(/[0-9a-zA-Z]/g,',')); // 所有的数字,小写字母,大写字母

        var str2 = '张三w李四F王五n马六';
        console.log(str2.replace(/[a-zA-Z]/g,'*'));// 大小写字母
        // i 不区分大小写
        console.log(str2.replace(/[a-z]/ig,'*'));// 大小写字母

 6. charAt(n) 跟直接使用[]是一样的

        var str2 = '张三w李四F王五n马六';
        console.log(str2.charAt(1));//三
        console.log(str2[0]);//张

7. charCodeAt获取指定字符的ascii编码

 var str1 = 'helloworld';
        var getStr1 = str1.charCodeAt(2);
        // 获取l字母 -> ASCII编码
        console.log(getStr1);
        var num = 108;
        console.log(String.fromCharCode(num)) // l

 8  toLowerCase() 转为小写    toUpperCase() 转为大写

        var str1 = 'Hello World';
        console.log(str1.toLowerCase());
        console.log(str1.toUpperCase());

 9. split(分割符,[返回数组的最大长度]) 将字符串分割为数组

        var str = 'I am student my name is jack'; 
        var str2 = '张三7李四8王五3马六5马六8马六';
        console.log(str.split()); // 没有参数值,会直接把字符串放入到数组中
        console.log(str.split(' ')); // 按照空格分隔
        console.log(str2.split(/[0-9]/));
        console.log(str2.split('')); //按照空字符串分割,每一个内容都分隔开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值