1.什么是String对象
String全局对象是一个用于字符串或一个字符序列的构造函数。一个字符串用于储存一系列字符就像“John Doe”,一个字符串可以使用单引号或双引号。
2.如何声明String对象
一般使用如下的几种方式声明string对象。一般就使用第一种就可以,第二种可以将其他类型的对象转换为String类型,第三种得到的是一个字符串对象
let str1 = "hello world"
let str2 = String(123)
let str3 = new String(456)
3.Date的常用方法
length
var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
console.log(str.length)
// 26
//在字符串中空格也算一个长度
indexOf()
let str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let index1 = str.indexOf('A')
let index2 = str.indexOf('Z')
let index3 = str.indexOf('a')
console.log(index1) //0
console.log(index2) //25
console.log(index3) //-1
//这里的索引也是从零开始,最后一位的索引为str.length-1. 查找的元素不存在则返回-1
match()
var str = "Hello world!"
let result1 = str.match("Hello")
let result2 = str.match("JS")
console.log(result1) //"Hello"
console.log(result2) //null
//用来查询是否含有该字符串,含有就返回该字符串
charAt()
let str = "Hello world!"
let result1 = str.charAt(1)
let result2 = str.charAt(0)
console.log(result1) // "e"
console.log(result2) // "h"
//不给索引的话,默认为0.如果给的索引超过了索引的最大值,或者小于0,那么都会返回空的字符串
concat()
let str1 = "Hello"
let str2 = " world!"
let str = str1.concat(str2)
console.log(str)
//"Hello world!"
//会使连个字符串合并称为一个新的字符串
"".concat(null) // "null"
"".concat(true) // "true"
"".concat(4, 5) // "45"
//也可以使用空的字符串直接使用concat方法。
replace()
let str = "Hello world!"
let str1 = str.replace("world","javascript")
console.log(str1)
//"Hello javascript"
//需要两个参数一个是需要替换的值(可以是字符串或者是一个正则表达式),第二个参数是需要被替换成的值(可以是一个字符串或者是一个每次匹配都要调用的回调函数)
includes()
let str1 = "Hello World"
let str2 = "Hello"
let str3 = "world"
let result1 = str1.includes(str2)
let result2 = str1.includes(str3)
console.log(result1) //true
console.log(result2) //false
//判断一个字符串中是否含有另一个字符串 str.includes(searchString[, position])
//[, position]表示需要检索的字符串,在str中的起始位置,默认为0,可选填。
repeat()
let str = 'abc'
let result1 = str.repeat(-1) // RangeError: repeat count must be positive and less than inifinity
let result1 = str.repeat(0) //""
let result1 = str.repeat(1) //"abc"
let result1 = str.repeat(2) //"abcabc"
let result1 = str.repeat(2.5) //"abcabc"
//表示该字符串以一定数量链接起来形成新的字符串。参数为负值或者为1/0这种不符条件的数时,直接报错。
//当参数以小数形式出现时,那么默认为小于该参数的最大整数值。
search()
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';
const regex = /[^\w\s]/g;
console.log(paragraph.search(regex));
// 输出: 43
console.log(paragraph[paragraph.search(regex)]);
// 输出: "."
//该方法用于返回查找元素对象的首字母位置。也就是说在找到复合要求的元素对象后返回第一个字母的索引。
slice()
const str = 'The quick brown fox jumps over the lazy dog.';
console.log(str.slice(31));
// 输出: "the lazy dog."
console.log(str.slice(4, 19));
// 输出: "quick brown fox"
console.log(str.slice(-4));
// 输出: "dog."
console.log(str.slice(-9, -5));
// 输出: "lazy"
//str.slice(beginIndex[, endIndex]) beginIndex表示开始的索引值,[, endIndex]表示结束的索引值,可选填。如果有该值不写,就默认提取到字符串尾。如果beginIndex为负值,那么就表示负值到句尾的字符串。
split()
var str="How are you doing today?";
const words = str.split(' ');
console.log(words[3]);
// 输出: "doing" [How,are,you,doing,today?]
const chars = str.split('');
console.log(chars[8]);
// 输出: "y" 空格也算一个元素 [H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?]
const strCopy = str.split();
console.log(strCopy);
// 输出: Array["The quick brown fox jumps over the lazy dog."]
//splice方法使用指定的分隔符字符串将一个String对象分割成子字符串数组,以一个指定的分割字串来决定每个拆分的位置 如果不填条件,或者填的条件不存在,那么就是一整个字符串是一个数组