<!Doctype html> <html xmlns="http://www./1999/xhtml"> <head> <title>字符串对象</title> <style type="text/css"> #show{ width:200px; height:200px; margin:0 auto; color:white; background:#2c2c2c; text-align:center }
</style>
<script type="text/javascript"> //创建字符串对象 //第一种方法: // var str=new String("hello")
//第二种方法: // var str="hello"
// 属性 length 求长度 /*方法 格式替代方法 操作方法 格式类的方法: Anchor big 大 small 小 bold 粗体 Italics 斜体 strike 横线 */ // var str="hello" var str = new String("hellow") document.write ("字符串的长度:"+str.length +"<br/>") document.write ("原始字符串:"+str +"<br/>") document.write ("粗体:"+str.bold().italics().strike().blink() +"<br/>") //返回的是字符串, 所以可以用属性 document.write ("粗体:<b>"+str+"</b><br/>") //效果一致 /*字符串操作方法 indexof (searchstring,position) 从 position(可选)位置开始,搜素字符中的第一个 searchstring 所出现的位置并返回 lastlndexof (searchstring,position) 从position(可选)位置开始,搜素字符中的最后一个 searchstring 所出现的位置并返回 charAt 返回在指定位置的字符。 charCodeAt 返回在指定的位置的字符的 Unicode 编码。 toLowerCase 把字符串转换为小写。 toUpperCase 把字符串转换为大写。 */ var sql="Hellow,world" document.write(sql+"<br/>")
var index=sql.indexOf("w",0) document.write("搜素Hellow,world中字符串出现的位置:"+index) // 搜素出现的位置0 1 2 3 4 5
var i=sql.lastIndexOf("o") document.write("<br/>Hellow,world搜素w最后出现的位置:"+i)
document.write("<br/><span>------------------------------------------<span>") // charAt 返回在指定位置的字符。 //charCodeAt 返回在指定的位置的字符的 Unicode 编码。 document.write("<br/>"+sql.charAt(7)) /// 第8个位置的字符。 document.write("<br/>"+sql.charCodeAt(7)) //放回对应的编码 document.write("<br/>小写字母"+sql.toLowerCase()) document.write("<br/>大写字母"+sql.toUpperCase()) </script>
</head>
<body> </body>
</html>
|