JavaScript字符串的属性和方法
1、属性(字符串的长度):
var str = 'hello world';
//属性 获取字符串的长度
document.write(str.length);//11
2、方法(简单列举十三个常用方法:):
①charAt()方法 (该方法可以返回指定位置的字符)根据字符串的索引返回对应字符串的内容
对应代码如下:
var str = 'hello world';
document.write(str.charAt(2));//l
document.write(str.charAt(str.length - 1));// d获取最后一个字符,当不知道字符串的长度时,就可以用这个
注意:
当不知道字符串的长度时,想要拿到字符串最后一个字符,可以用str.length-1来获取。(因为字符串的索引是从0开始的)
②indexOf()方法 (该方法可返回某个指定的字符串值在字符串中首次出现的位置)
对应代码如下:
//indexOf()方法可返回某个指定的字符串值在字符串中首次出现的位置
var str = 'hello world';
document.write(str.indexOf('o'));//4
document.write(str.indexOf('a'));//-1如果里面没有匹配成功则返回-1,匹配成功则返回对应的位置信息,用途:可用于判断字符串里面有没有对应的字符串值 。并且区分大小写
document.write(str.indexOf('o', 5));//7 第一个参数对应要查找的值,第二个参数开始查找的位置
注意:
A、可以根据该方法返回值为-1判断符串里面有没有对应的字符串值
B、这个字母是区分大小写的
C、当有两个参数时,应注意对应参数表示的意思。第一个参数表示要查找的字符串值,第二个参数表示开始查找的位置。
③lastIndexOf()方法可返回一个指定的字符串值最后出现的位置
对应代码如下:
var str = 'hello world';
document.write(str.lastIndexOf('o'));//7
document.write(lastIndexOf('o', 5));//4 先将其(0-5)的字符串截取出来,然后再从后往前数
注意:
当有两个参数时,第一个参数表示要查找的字符串的值,第二个参数表示的是在字符串中开始检索的位置。这个与indexOf()方法不同注意两者的区别代码中已标出不同。
④substr(start,length)方法可在字符串中抽取从开始下标开始的指定数目的字符
对应代码如下:
var str = 'hello world';
document.write(str.substr(4))//o world
document.write(str.substr(0,4))// hell substr(start,length)
⑤substring(from,to)方法用于提取字符串中介于两个指定下标之间的字符
对应代码如下:
var str = 'hello world';
document.write(str.substring(4))//o world
document.write(str.substring(0, 4))//hell substring(from,to)
注意:
A、如果参数 from 与 to 相等,那么该方法返回的就是一个空串(即长度为 0 的字符串)。
B、如果 from比 to 大,那么该方法在提取子串之前会先交换这两个参数。
C、如果to的参数为负数则均看为0;
⑥slice(start,end)方法可提取字符串的某个部分,并以新的字符串返回被提取的部分
对应代码如下:
var str = 'hello world';
document.write(str.slice(2, 4));// ll slice(start,end)
document.write(str.slice(-1));// d -1表示最有一个字符串
substring()和slice()的区别:
A、substring()这个方法参数一般不允许负数(即使是负数那也是是被看做0来处理的)
B、slice()方法参数允许负数,切当为-1时代表最后一个字符串,一次类推下去。
代码如下:
var str = 'hello world';
document.write(str.substring(3, -1))
document.write(str.slice(3, -4));//lo w
document.write(str.substring(3, -4));//hel 总结:当参数为负数时,则代表0
注意:
④⑤⑥这三个方法,是字符串的截取方法,注意他们之间的区别。
⑦split() 参数是以什么方式来分割成数组 方法用于把一个字符串分割成字符串数组
对应代码如下:
var str3 = "how,are,you";
str3.split(",");//["how","are","you"]
var str3 = "how are you";
str3.split(" ");//["how","are","you"]
var str3 = "how are you";
str3.split("");//["h", "o", "w", " ", "a", "r", "e", " ", "y", "o", "u"]
document.write(str3.split(",", 2))//["how","are"] 2表示数组的最大长度
注意: 这个方法用于将字符串转换为数组形式,便于取值。
⑧replace() 方法用于在字符串中用一些字符替代另一些字符 或这替换与正则表达式匹配的子串
对应代码如下:
var str2 = "hello amy";
// replace(searchValue, newValue) 返回的是新的字符串 两个参数
var s = str2.replace('hello', 'hi');
document.write(s);//hi amy
⑨includes()方法用于判断字符串是否后包含指定的字符串 返回布尔类型 true false
对应代码如下:
var str = 'hello world';
document.write(str.includes('a'));//false
document.write(str.includes('hello'));//true
注意:
这个方法返回的值是布尔类型,同时判断字符串值在指定字符串中是否存在,还可以用前面提到到的indexOf()方法,当其返回值为-1时则表示不存在,否则存在。
⑩concat()方法用于连接多个字符串
对应代码如下:
var str1 = "hello";
var str2 = " lily";
document.write(str1.concat(str2, ' welcome'));//hello lily welcome 要预留空格才不会黏在一起
注意: 多个字符串连接想要中间预留空间,使其更美观,则就必须引号前或引号后预留空格。
十、concat()方法用于连接多个字符串
对应代码如下:
var str1 = "hello";
var str2 = " lily";
document.write(str1.concat(str2, ' welcome'));//hello lily welcome 要空格才不会黏在一起
十一、toLowerCase() 方法用于把字符串转换为小写
对应代码如下:
var str4 = "HELLO";
document.write(str4.toLowerCase());//hello
十二、/torUpperCase() 方法用于把字符串转换为大写
对应代码如下:
//torUpperCase() 方法用于把字符串转换为大写
var str4 = "hello world";
document.write(str4.toUpperCase());//HELLO WORLD
十三、 //trim()方法用于删除字符串的头尾空格
对应代码如下:
//trim()方法用于删除字符串的头尾空格
var str5 = " hello ";
document.write(str5.trim());//hello