字符串方法:所有的字符串方法都是返回新的字符串,不会更改原始字符
repeat()
<script>
// repeat()-实例方法
// 表示将元字符串重复n次,返回一个新字符串
// 参数:数值
let str='7'
console.log(str.repeat(3)); //777
console.log('h'.repeat(6)); //hhhhhh
</script>
startsWith()和endsWith()
<script>
// startsWith():表示参数字符串是否在原字符串的头部,返回布尔值
// endtsWith():表示参数字符串是否在原字符串的尾部,返回布尔值
let str='abcde'
console.log(str.startsWith('abd')); //false
console.log(str.startsWith('ab')); //true
console.log(str.endsWith('e')); //true
console.log(str.endsWith('de')); //true
</script>
length:返回字符串长度
<script>
var str='ebcao1nl@9010'
console.log(str.length); // 13
</script>
字符串查找:indexOf()、lastindexOf()
indexOf()返回字符串中指定文本首次出现的索引(位置),未出现,返回-1 ;
可接收两个参数,第一个参数是查找的内容,第二个参数,可传可不穿,从指定位置开始
lastIndexOf() 返回指定文本在字符串中最后一次出现的索引,未出现,返回-1 ;
可接收两个参数,第一个参数是查找的内容,第二个参数,可传可不穿,从开头到指定位置
<script>
var str = "The full name of China is the People's Republic of China.";
var a = str.indexOf("China");
var c = str.indexOf("China",18);
console.log(a); // 17
console.log(c); // 51
var b = str.lastIndexOf("China");
console.log(b); // 51
</script>
search()
搜索特定值的字符串,并返回匹配的位置.未出现,返回-1
var str = "The full name of China is the People's Republic of China.";
var a = str.search("China");
var b = str.search("love");
console.log(a); // 17
console.log(b); // -1
slice()
提取字符串的某个部分并在新字符串中返回被提取的部分,俩参数:起始索引,终止索引,不改变原来的字符串,参数可以是负数,负数从后面开始截取字符串,如果只传一个参数,就是从该参数的索引到最后的全部字符
<script>
var str = "hello,World!";
// slice(起始索引,终止索引) 不改变原字符
// 为两个参数的情况:
// 1.正数:第一个参数和第二个参数都是正数的时候,截取的是(起始索引,终止索引-1)
// 2.负数:第一个参数和第二个参数都是负数的时候,截取的是(起始索引,终止索引-1)
// 为一个参数的情况:
// 1.正数:截取的是 起始索引到最后的全部字符
// 2.负数:截取的是 从结尾数起最后的全部字符
console.log(str.slice(6,11)); //World
console.log(str.slice(-6,-1)); //World
console.log(str.slice(6)); //World!
console.log(str.slice(-6)); //World!
</script>
substring():与slice()方法一样,也不改变原数组,但没有办法接受负数
<script>
var str = "hello,World!";
// slice(起始索引,终止索引) 不改变原字符,与slice基本相似,但不能接受负数
// 为两个参数的情况:
// 1.正数:第一个参数和第二个参数都是正数的时候,截取的是(起始索引,终止索引-1)
// 为一个参数的情况:
// 1.正数:截取的是 起始索引到最后的全部字符
console.log(str.substring(6,11)); //World
console.log(str.substring(6)); //World!
console.log(str); // hello,World!
</script>
substr(起始位置,切割长度):第二个参数代表的是长度,不能为负
<script>
var str = "hello,World!";
// substr(起始索引,切割长度) 不改变原字符 第二个参数代表的是长度,不能为负
console.log(str.substr(6,4)); //Worl
console.log(str.substr(-12,6)); //hello,
console.log(str); // hello,World!
</script>
replace()
<script>
// replace() 用另一个值替换在字符串中指定的值
// replace() 方法不会改变调用它的字符串。它返回的是新字符串。
var str = "hello,World!";
console.log(str.replace('hello','你好')); //你好,World!
console.log(str); //hello,World!
// replace() 默认只替换首个匹配项,如需替换所有匹配项,应使用正则表达式 /g
str1="hello,hello,world!"
console.log(str1.replace('hello','你好')); //你好,hello,world!
console.log(str1.replace(/hello/g,'你好')); //你好,你好,world!
// replace() 对大小写敏感,如需执行大小写不敏感的替换,应使用正则表达式 /i
console.log(str.replace('Hello','你好')); //hello,World!
console.log(str.replace(/Hello/i,'你好')); //你好,World!
</script>
split()
<script>
// split() 将字符串转换为数组
var txt = "a,b,c,d,e"; // 字符串
console.log(txt.split(",")); // 用逗号分隔 ['a', 'b', 'c', 'd', 'e']
console.log(txt.split(" ")); // 用空格分隔 ['a,b,c,d,e']
console.log(txt.split("|")); // 用竖线分隔 ['a,b,c,d,e']
// 如果省略分隔符,被返回的数组将包含 index[0] 中的整个字符串
// 如果分隔符是 “”,被返回的数组将是间隔单个字符的数组
var txt1 = "Hello"; // 字符串
console.log(txt1.split("")); // 分隔为字符 ['H', 'e', 'l', 'l', 'o']
</script>
大小写转换——toUpperCase()、toLowerCase()
<script>
var text = "Hello World!";
// toUpperCase() 将字符串转换为大写
console.log(text.toUpperCase()); //HELLO WORLD!
// toLowerCase() 将字符串转换为小写
console.log(text.toLowerCase()); //hello world!
</script>
concat()
<script>
// concat() 连接两个或多个字符串
var text1 = "Hello";
var text2 = "World";
console.log(text1.concat("!", text2));
// concat() 方法可用于代替加运算符。下面两行是等效的:
var text = "Hello" + " " + "World!";
var text = "Hello".concat(" ", "World!");
</script>
trim()
<script>
// trim() 方法删除字符串两端的空白符:
var str = " Hello,World! ";
console.log(str); // Hello,World!
console.log((str.trim())); // Hello,World!
</script>
提取字符串字符——chart()、charCodeAt()
<script>
// charAt() 方法返回字符串中指定下标(位置)的字符串
var str = "HELLO WORLD";
console.log(str.charAt(3)); // L
// charCodeAt() 方法返回字符串中指定索引的字符 unicode 编码
var str = "HELLO WORLD";
console.log(str.charCodeAt(5)); // 32
</script>
includes()
<script>
// includes('查找项') 以指定字符开头,是返回true,否则返回false
var str='hello world!'
console.log(str.includes(' ')); //true
console.log(str.includes('r')); //true
console.log(str.includes('t')); //fale
</script>