示例代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>字符串对象</title>
<style type="text/css">
*{margin:0}
#show{
width:200px;
height:200px;
margin:0 auto;
color:white;
background:#2c2c2c;
text-align:center
}
</style>
<script type="text/javascript">
//var str = "hellow";
var str = new String("hellow");
document.write("<div id='show'>");
document.write("字符串的长度:"+str.length+"<br/>");
document.write("原始字符串的样式"+str+"<br/>");
document.write("粗体:"+str.bold().italics().strike()+"<br/>");
document.write("粗体:<b>"+str+"</b><br/>");
document.write("</div>");
</script>
</head>
<body>
</body>
</html>
效果如下:
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>字符串方法</title>
<style type="text/css">
*{margin:0}
#show{
width:200px;
height:200px;
margin:0 auto;
color:white;
background:#2c2c2c;
text-align:center
}
</style>
<script type="text/javascript">
var str = "Hello,world";
document.write("<div id='show'>");
var index = str.indexOf("o",5);
var i = str.lastIndexOf("o");
document.write("Hello在字符串中出现的位置为:"+index+"<br/>");
document.write("o在字符串中最后出现的位置为:"+i);
document.write("<br/>"+str.charAt(2));
document.write("<br/>"+str.charCodeAt(2));
document.write("<br/>大写字母为:"+str.toLowerCase());
document.write("</div>");
</script>
</head>
<body>
</body>
</html>
结果如下:
示例代码入下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>字符串方法</title>
<style type="text/css">
*{margin:0}
#show{
width:200px;
height:200px;
margin:0 auto;
color:white;
background:#2c2c2c;
text-align:center
}
</style>
<script type="text/javascript">
var str = "a,b,c,d";
var str1="abcd";
var a = str.split(",");
var temp = "";
document.write("<div id='show'>");
for(var index in a){
temp += "数组中第"+(parseInt(index)+1)+"个元素:"+a[index]+"<br/>";
}
document.write(temp);
document.write("<br/>slice:"+str1.slice(1,3));
document.write("<br/>substring:"+str1.substring(1,3));
document.write("<br/>substr:"+str1.substr(1,3));
document.write("</div>");
</script>
</head>
<body>
</body>
</html>
结果如下: