字符的一些操作方法

字符是个字符,但是也可以使用类似于对象的操作方式,所以说字符也有方法,也有属性。那么它们都分别是谁,有什么作用呢?下面让我给你一一道来。

老规矩,我们先定义一个字符串,来为下面的属性和方法使用。

var str= “Hello World”;
1. str.length

//参数:无
//返回值:无
//功能:计算字符串中字符的个数,长度
//是否改变原来字符串:否
 console.log(str.length); //11

2.str.replace()

// 参数:两个,1老的子串,2要替换成的新串
// 返回值:替换之后的新字符串
// 功能:替换,并只能替换第一个符合的子串
// 是否改变原字符:否   
var res = str.replace("H","啊");
console.log(res); //啊ello World

拓展:如何把字符串中的某个字符全部更改

for(var i = 0;i<str.length;i++){
       	str = str.replace("l","啊");
       }
       console.log(str); //He啊啊o Wor啊d

3.str.indexOf()

   // 参数:一个或两个,当参数为一个时,是从左往右查找第一个符合参数字符所在的索引,当有两个参数时,第二个参数代表从此索引开始查找
    // 返回值:符合条件的字符的索引,没有符合字符时,返回-1
    // 功能:查找目标字符所对应的索引
    // 是否改变原字符:否   
    // var res = str.indexOf("l");// 2
       var res = str.indexOf("l",5); //9
       console.log(res);

4.str.lastIndexOf()

// 参数:一个或两个,当参数为一个时,是从右往左查找第一个符合参数字符所在的索引,当有两个参数时,第二个参数代表从此索引开始查找
    // 返回值:符合条件的字符的索引,没有符合条件时,返回-1
    // 功能:查找目标字符所对应的索引
    // 是否改变原字符:否   
//     var res = str.lastIndexOf("l");// 9
       var res = str.indexOf("l",5); // 9
       console.log(res);

5.str.slice()

 // 参数:一个或两个,当参数为一个时,为截取参数索引到最后索引之间的字符,两个参数时,表示从第一个参数索引到第二个参数索引之间截取(注意是取不到第二个参数索引[2,7))
    // 返回值:截取的字符
    // 功能:截取字符串
    // 是否改变原字符:否   
    var res = str.slice(2,7);
    console.log(res);//llo W

6.str.substring()

// 参数:一个或两个,当参数为一个时,为截取参数索引到最后索引之间的字符,两个参数时,表示从第一个参数索引到第二个参数索引之间截取(注意是取不到第二个参数索引[2,7))
    // 返回值:截取的字符
    // 功能:截取字符串
    // 是否改变原字符:否  
       var res = str.substring(2,7);
       console.log(res);//llo W

我们明显可以发现这个方法与上面slice()的功能是一样的

7.str.substr()

  // 参数:一个或两个,当参数为一个时,为截取参数索引到最后索引之间的字符,两个参数时,第二个参数指的是截取的个数
    // 返回值:截取的字符
    // 功能:截取字符串
    // 是否改变原字符:否  
       var res = str.substr(2,7);//llo Wor     
       console.log(res);

这个我们可以发现,与上面的两种方法相比,只是第二个参数所对应的功能不一样。但是此方法主要也是为了截取字符串。

8.str.toUpperCase()

       // 参数:无
       // 返回值:改变为大写字母后的字符串
       // 功能:将不是大写标志的字符改为大写格式
       // 是否改变原字符:否
       var res = str.toUpperCase();
       console.log(res);//HELLO WORLD

9. toLowerCase()

       // 参数:无
       // 返回值:改变为小写字母后的字符串
       // 功能:将不是小写标志的字符改为小写格式
       // 是否改变原字符:否
       var res =  str.toLowerCase();
       console.log(res);//hello world

10.str.concat()

       // 参数:另一个字符串
       // 返回值:合并后的字符串
       // 功能:将两个字符串进行合并
       // 是否改变原字符:否
       var str = "hello";
       var str2 = "world";
       var res = str.concat(str2);
       console.log(res);//helloworld

11.str.split()

     // 参数:如下例子
       // 返回值:改变后的字符串,并且存在了数组内
       // 功能:按照指定自字符,分割字符串,分割成数组
       // 是否改变原字符:否
       var str = "abc-qwe-asd-zxc";
//     var res = str.split();//["abc-qwe-asd-zxc"]
//     var res = str.split("");//["a", "b", "c", "-", "q", "w", "e", "-", "a", "s", "d", "-", "z", "x", "c"]
       var res = str.split("-");//["abc", "qwe", "asd", "zxc"]
       console.log(res);

12.str.charAt()

       // 参数:字符串中的索引
       // 返回值:对应的字符
       // 功能: 根据索引查数据,当前索引没有数据,返回空字符
       // 是否改变原字符:否
//     var res = str.charAt(1);//e
       var res = str.charAt(50);//空字符
       console.log(res);
       
发布了15 篇原创文章 · 获赞 10 · 访问量 490

猜你喜欢

转载自blog.csdn.net/weixin_43797492/article/details/104736796