<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body> <script> //1.charAt() 查找返回指定位置的字符 var str = "abcdefg"; var s = str.charAt(5); // 下标为5的字符 console.log(s); //f // 2.charcodeAt()查找返回指定位置字符的unicdoe码 var str = "abcdefg"; var s = str.charCodeAt(5); // 下标为5的字符的unicdoe码 console.log(s); //102 // 3.concat()连接字符串 var str = "abc"; var str1 = str.concat("1", "2", "3", "dsdafsdafsad"); console.log(str1); //abc123dsdafsdafsad // 4.slice()字符串截取 var str = "abcdefghijklmn"; var str1 = str.slice(1, 5); console.log(str1); //bcde // 5.split()将字符串转换为数组 var str = "abcdefg"; var str1 = str.split("-"); var str2 = str.split(""); console.log(str); //abcdefg console.log(str1); //["abcdefg"] console.log(str2); //["a", "b", "c", "d", "e", "f", "g"] // 6.substring()字符串截取 var str = "abcdefghijklm"; var str1 = str.substring(1, 3); //从下边1开始到下边3结束, 不包括下标3那一项 var str2 = str.substring(3, 1); console.log(str1); //bc console.log(str2); //bc // 7.substr()字符串截取 var str = "abcdefghijklm"; var str1 = str.substr(2, 6); //从下标2开始往后数6个数 console.log(str1); //cdefgh // 8.indexOf() 遍历字符串 查找包含第一个参数的字符串,如包含返回第一位的下标,停止查找,如没有返回 -1。第二个参数表示从此下标开始查找。 var str = "abcdefabcjklm"; var a = str.indexOf("abc"); var b = str.indexOf("abc", 1); console.log(a); //0 console.log(b); //6 // 9.toUpperCase 转大写 var str = "aAbBcCdD"; console.log(str.toUpperCase()); //AABBCCDD // 10.toLowerCase 转小写 var str = "aAbBcCdD"; console.log(str.toLowerCase()); //aabbccdd // 11.replace()替换字符串 只能匹配一次 var str = "今天天天天气好好"; var str1 = str.replace("天天", "**"); console.log(str1); //今**天天气好好 // 12.match()查找字符串 var str = "今天天气好好"; var result = str.match("天天"); console.log(result); //["天天", index: 1, input: "今天天气好好", groups: undefined] var st = str.match("金"); console.log(st); // null // 13.search() 查找字符串返回下标 var str = "今天天气天天好好"; var result = str.search("气"); console.log(result); //3 var res = str.search("地"); console.log(res); //-1 // 14.startsWith 判定一个字符串是否以另一个字符串开头 var str = "hello world"; var result = str.startsWith("he"); console.log(result); //true var result = str.startsWith("he", 1); //当为两个参数时,第二个表示开始位数。 console.log(result); //flase // 15.endsWith 判定一个字符串是否以另一个字符串结尾 var str = "hello world"; //检测尾部 var result = str.endsWith("world"); console.log(result); //true //检测指定位置是否以指定字符结尾 var result1 = str.endsWith("wo", 8); console.log(result1); //true // 16.includes 检测是否包含指定字符串 var str = "hello world"; var result = str.includes("o"); console.log(result); //true var result1 = str.includes("o", 8); console.log(result1); //false var res = str.includes("o", 7); console.log(res); //true // 17.repeat 重复字符串 var str = "haha"; var str1 = str.repeat(3); console.log(str1); //hahahahahaha </script> </body> </html>
若有错误或纰漏请指正
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
//1.charAt() 查找返回指定位置的字符
var str = "abcdefg";
var s = str.charAt(5); // 下标为5的字符
console.log(s); //f
// 2.charcodeAt()查找返回指定位置字符的unicdoe码
var str = "abcdefg";
var s = str.charCodeAt(5); // 下标为5的字符的unicdoe码
console.log(s); //102
// 3.concat()连接字符串
var str = "abc";
var str1 = str.concat("1", "2", "3", "dsdafsdafsad");
console.log(str1); //abc123dsdafsdafsad
// 4.slice()字符串截取
var str = "abcdefghijklmn";
var str1 = str.slice(1, 5);
console.log(str1); //bcde
// 5.split()将字符串转换为数组
var str = "abcdefg";
var str1 = str.split("-");
var str2 = str.split("");
console.log(str); //abcdefg
console.log(str1); //["abcdefg"]
console.log(str2); //["a", "b", "c", "d", "e", "f", "g"]
// 6.substring()字符串截取
var str = "abcdefghijklm";
var str1 = str.substring(1, 3); //从下边1开始到下边3结束, 不包括下标3那一项
var str2 = str.substring(3, 1);
console.log(str1); //bc
console.log(str2); //bc
// 7.substr()字符串截取
var str = "abcdefghijklm";
var str1 = str.substr(2, 6); //从下标2开始往后数6个数
console.log(str1); //cdefgh
// 8.indexOf() 遍历字符串 查找包含第一个参数的字符串,如包含返回第一位的下标,停止查找,如没有返回 -1。第二个参数表示从此下标开始查找。
var str = "abcdefabcjklm";
var a = str.indexOf("abc");
var b = str.indexOf("abc", 1);
console.log(a); //0
console.log(b); //6
// 9.toUpperCase 转大写
var str = "aAbBcCdD";
console.log(str.toUpperCase()); //AABBCCDD
// 10.toLowerCase 转小写
var str = "aAbBcCdD";
console.log(str.toLowerCase()); //aabbccdd
// 11.replace()替换字符串 只能匹配一次
var str = "今天天天天气好好";
var str1 = str.replace("天天", "**");
console.log(str1); //今**天天气好好
// 12.match()查找字符串
var str = "今天天气好好";
var result = str.match("天天");
console.log(result); //["天天", index: 1, input: "今天天气好好", groups: undefined]
var st = str.match("金");
console.log(st); // null
// 13.search() 查找字符串返回下标
var str = "今天天气天天好好";
var result = str.search("气");
console.log(result); //3
var res = str.search("地");
console.log(res); //-1
// 14.startsWith 判定一个字符串是否以另一个字符串开头
var str = "hello world";
var result = str.startsWith("he");
console.log(result); //true
var result = str.startsWith("he", 1); //当为两个参数时,第二个表示开始位数。
console.log(result); //flase
// 15.endsWith 判定一个字符串是否以另一个字符串结尾
var str = "hello world";
//检测尾部
var result = str.endsWith("world");
console.log(result); //true
//检测指定位置是否以指定字符结尾
var result1 = str.endsWith("wo", 8);
console.log(result1); //true
// 16.includes 检测是否包含指定字符串
var str = "hello world";
var result = str.includes("o");
console.log(result); //true
var result1 = str.includes("o", 8);
console.log(result1); //false
var res = str.includes("o", 7);
console.log(res); //true
// 17.repeat 重复字符串
var str = "haha";
var str1 = str.repeat(3);
console.log(str1); //hahahahahaha
</script>
</body>
</html>