<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="box"></div>
<script type="text/javascript">
var str = "hello";
var box = document.getElementById("box")
/*字符串方法*/
/*1.字符串连接*/
{
let str0 = str.concat("world")
console.log(str0)
}
/*2.*/
{
let str0 = str.big();
console.log(str0)
}
/*3.chartAt*/
{
/*返回字符串对应位置的字符*/
console.log(str.charAt(2))//l
}
/*3.chartCodeAt*/
{
/*返回字符串对应位置的字符的XX码*/
console.log(str.charCodeAt(2))//108
}
/*4.includes*/
{
/*返回字符串是否包含字符*/
console.log(str.includes("o"))//true
console.log(str.includes("lo"))//true
console.log(str.includes("eo"))//false
}
/*5.indexOf*/
{
/*返回字符串是否包含字符,包括返回字符的位置,不包括返回-1*/
console.log(str.indexOf("o"))//4
console.log(str.indexOf("lo"))//3
console.log(str.indexOf("eo"))//-1
}
/*5.lastIndexOf*/
{
var str1 = "hellohello";
/*返回字符串是否包含字符*/
/*str.lastIndexOf(XX,N)从第N个位置倒着查找,有返回字符在字符串中的索引,没有返回-1*/
console.log(str1.lastIndexOf("o",6))//4
console.log(str1.lastIndexOf("lo",3))//3 索引3的位置是l而后边跟着的是o。满足要求,返回l的索引 3
console.log(str1.lastIndexOf("lo",2))//-1
console.log(str1.lastIndexOf("lo",9))//8
console.log(str1.lastIndexOf("eo",2))//-1
}
/*6.length返回字符串的长度*/
{
var str1 = "hellohello";
console.log(str1.length)//10
}
/*6.match*/
{
var str1 = "hellohello";
console.log(str1.match("o"))//["o", index: 4, input: "hellohello"]
console.log(str1.match(/o/))//["o", index: 4, input: "hellohello"]
/*当正则是全局的时候,返回的是查找到的项的集合*/
console.log(str1.match(/o/g))//["o", "o"]
var S = "huymgloymjd";
console.log(S.match(/(ym)|(jd)/g))//["ym", "ym", "jd"]
}
/*7.replace*/
{
/*字符替换*/
var str1 = "hellohello";
console.log(str1.replace("o","X"))//hellXhello
console.log(str1.replace(/o/g,"X"))//hellXhellX
}
/*8.search*/
{
/*在字符串中查找字符,返回索引值,不存在返回-1*/
var str1 = "hellohello";
console.log(str1.search("o"))//4
console.log(str1.search("p"))//-1
}
/*8.slice*/
{
var str1 = "hellohello";
/*返回一个新的数组,包含从 start 到 end (不包括该元素)。*/
console.log(str1.slice(1,5))//ello
console.log(str1.slice(2))//llohello
console.log(str1.slice(-3,-1))//ll 若为负数先加上长度在计算
console.log(str1.slice(-3,0))//空
console.log(str1.slice(-3))//llo
}
/*9.split*/
{
var str1 = "hellohello";
/*返回一个新的数组,包含从 start 到 end (不包括该元素)。*/
console.log(str1.split(""))//["h", "e", "l", "l", "o", "h", "e", "l", "l", "o"]
console.log(str1.split(","))//["hellohello"]
console.log(str1.split("l"))//["he", "", "ohe", "", "o"]
}
/*10.substr*/
{
var str1 = "hellohello";
/*返回一个新的数组,包含从 start开始,length长度 。*/
console.log(str1.substr(0,2))//he
console.log(str1.substr(1))//ellohello
console.log(str1.substr(-3))//llo
}
/*10.substring*/
{
var str1 = "hellohello";
/*返回一个新的数组,包含从 start 到 end (不包括该元素)。*/
console.log(str1.substring(0,2))//he
console.log(str1.substring(1))//ellohello
console.log(str1.substring(-3))//hellohello
/*重要事项:与 slice() 和 substr() 方法不同的是,substring() 不接受负的参数。*/
}
/*11.toUpperCase转大写*/
{
var str1 = "hellohello";
console.log(str1.toUpperCase())//HELLOHELLO
}
/*12.toLowerCase转小写*/
{
var str1 = "HJHGLS";
console.log(str1.toLowerCase())//hjhgls
}
</script>
</body>
</html>