String-JavaScript

来源:互联网 发布:php书籍推荐 知乎 放弃 编辑:程序博客网 时间:2024/05/16 12:46

这是一篇关于JavaScript中String对象的博客。


!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">
<title>String</title>
<script type="text/javascript">
var str1=new String("hello");
var str2=new String("hello"); //
两个对象各开辟了一个新地址;
document.write("结果:"+(str1==str2)+"</br>"); //故输出结果为false;
document.write("结果:"+(typeof(str1)==typeof(str2))+"</br>");
var str1="hello";
var str2="hello"; //两个对象为同一地址;
document.write("结果:"+(str1==str2)+"</br>"); //故输出结果为true;
var str="hellojava";
document.write(str.charAt(4)+"</br>"); //查找索引对应的项;
document.write(str.indexOf("a")+"</br>"); //查找项对应的索引;
document.write(str.lastIndexOf("a")+"</br>");
document.write(str.fontcolor("red")+"</br>");
document.write(str.replace("hello","hate")+"</br>"); //替换;
document.write(str.substring(5,9)+"</br>"); //截取;
document.write(str.substr(5,4)+"</br>");
var str="邱-恒-小-猪";
var strArray=str.split("-")
for(i=0;i<strArray.length;i++)
{
document.write(strArray[i]+",");
} //用-分割;
</script>
</head>
<body>
</body>
</html>