JavaScript系列---【字符串对象(String)--知识点总结】

字符串对象 (String)

每一个字符串都属于字符串类的实例

用单引或双引包起来的就是字符串

字符串的创建

// 直接赋值 字面量
var str = "哈哈abcdefghijklmnab123456";

// 构造函数(实例的方式)
var str1 = new String("哈哈abcdefghijklmnab123456");

字符串对象的属性方法

原则:字符串的所有操作都不会修改原来字符串

字符串的获取
  • length

    • 作用: 当前字符串的长度或者字符的个数
    • 返回值:长度
     console.log(str.length);//长度24
    
  • charAt(index);

    • 作用:获取对应索引位置的字符串
    • index:索引
    • 返回值:返回对应的字符串
     console.log(str.charAt(0));//哈
    
  • charCodeAt(index);

    • 作用:获取对应索引位置的Uncode编码 ; ASCII值 (返回一个整数值范围是0-65535)
    • index:索引
    • 返回值:(返回一个整数值范围是0-65535)
       console.log(str.charCodeAt(0));//哈的Uncode编码为21704
    
  • indexOf(字符串);

    • [index]:从那个位置开始查找,不传递这个参数默认从索引为0开始进行查找 (从前往后)
    • 作用:判断当前字符串首次出现的索引(判断当前这个字符串是否包含另一个字符串)
    • 返回值:包含当前这个字符串返回首次出现的索引,不包含返回-1
     console.log(str.indexOf("a"));//索引2
     console.log(str.indexOf("def"));//索引5
     console.log(str.indexOf("de呵呵"));//-1
     console.log(str.indexOf("ab"));//索引2
     console.log(str.indexOf("ab", 5));//索引16
    
  • lastIndexOf(字符串,[index]);

    • [index]:从那个位置开始查找,默认是从末尾开始进行查找(从后往前找)
    • 作用:判断当前字符串最后一次出现的索引(判断当前这个字符串是否包含另一个字符串)
    • 返回值:包含当前这个字符串返回最后一次出现的索引,不包含返回-1
     console.log(str.lastIndexOf("ab"));//索引16
     console.log(str.lastIndexOf("ab", 1));//-1
     console.log(str.lastIndexOf("1", 21));//18
     console.log(str.lastIndexOf("1", 10));//-1
    
字符串的截取

原则:返回值都是字符串,截取到就返回对应字符,截取不到返回空字符串

  • substring(startIndex,endIndex);
    • 作用:字符串截取
    • 参数: startIndex:开始索引 endIndex:结束索引 (不包含结束索引)
    • 注意点
      • 不传递参数默认从索引为0开始截取到末尾
      • 值传递第一个参数,从当前索引开始截取到末尾
      • 不支持负数索引,第一个参数传递负数索引会变为默认值0,两个参数都为负返回空字符串
console.log(str.substring());//哈哈abcdefghijklmnab123456
console.log(str.substring(0));//哈哈abcdefghijklmnab123456
console.log(str.substring(1));//哈abcdefghijklmnab123456
console.log(str.substring(2, 6));//abcd
console.log(str.substring(-1));//哈哈abcdefghijklmnab123456

console.log(str.substring(-5, -2));//''
console.log(str.substring(100, 200));//''
  • substr(startIndex,length);
    • 作用:字符串截取
    • 参数:startIndex:开始索引 length:长度
    • 注意点
      • 不传递参数默认从索引为0开始截取到末尾
      • 值传递第一个参数,从当前索引开始截取到末尾
      • 第一个参数支持负数索引,第二个参数表示长度不支持负数索引
console.log(str.substr());//哈哈abcdefghijklmnab123456

console.log(str.substr(0));//哈哈abcdefghijklmnab123456

console.log(str.substr(1));//哈abcdefghijklmnab123456

console.log(str.substr(1, 3));//哈ab
console.log(str.substr(-5, 3)); //234
console.log(str.substr(-5, -3)); //''
console.log(str.substr(-8)); //ab123456
  • slice(startIndex,endIndex);
    • 作用:字符串截取
    • 参数:startIndex:开始索引 endIndex:结束索引 (不包含结束索引)
    • 注意点
      • 不传递参数默认从索引为0开始截取到末尾
      • 值传递第一个参数,从当前索引开始截取到末尾
      • 两个参数都支持负数索引,如果两个参数都是负数第一个参数一定要小于第二个参数否则返回空字符串
 console.log(str.slice());//哈哈abcdefghijklmnab123456

 console.log(str.slice(0));//哈哈abcdefghijklmnab123456

 console.log(str.slice(1));//哈abcdefghijklmnab123456
 console.log(str.slice(-5));//23456
 console.log(str.slice(-5,-2));//234
 console.log(str.slice(-5,-8));//""
  • toUpperCase();
    • 作用:转为大写
  var str = "abcdefABC";
   console.log(str.toUpperCase());//"ABCDEFABC"
  • toLowerCase();
    • 作用:转为小写
var str = "abcdefABC";
console.log(str.toLocaleLowerCase());//"abcdefabc"
  • split("分割字符串");
    • 作用:将当前这个字符串以某个字符串进行分割
    • 返回值:返回值一个数组
var str3 = "abc&def&lkj&jkl";
console.log(str3.split("&"));  // ["abc", "def", "lkj", "jkl"]
console.log(str3);
  • replace(searchString,replaceString);
    • 作用:将字符串当前的查找字符串进行替换(调用一次这个方法只能替换一处,首次出现的位置)
    • 参数:
      • searchString:查找字符串
      • replaceString:替换字符串
 var str5 = "abcdefgdefaaadefmmm";
 var resStr5 = str5.replace("def","哈哈");
 console.log(resStr5);//abc&def&lkj&jkl
  • trim();
    • 作用:去除收尾空格
var str6 = "   你好 我好 大家好   ";
console.log(str6.trim());
原文地址:https://www.cnblogs.com/chenhaiyun/p/14551215.html